How to load JSON assets into Flutter App

前端 未结 4 1347
渐次进展
渐次进展 2020-12-15 02:40

How do I load a JSON asset into my Flutter app?

My pubspec.yaml file has the following:

  assets:
    - assets/data.json
4条回答
  •  心在旅途
    2020-12-15 03:16

    You can use this code :)

    loadJson() async {
      String data = await rootBundle.loadString('assets/json/keyboard.json');
      jsonResult = json.decode(data);
      print(jsonResult);
    }
    

    Can load on start :)

      @override
      void initState() {
        super.initState();
        WidgetsBinding.instance.addPostFrameCallback((_) async {
          await loadJson();
        });
      }
    

    Need to add the JSON on the asset

    flutter:
      uses-material-design: true
      assets:
        - assets/json/keyboard.json
    

提交回复
热议问题