How do I load a JSON asset into my Flutter app?
My pubspec.yaml
file has the following:
assets: - assets/data.json
I keep getting stuck trying to load the data. I tried:
final json = JSON.decode( DefaultAssetBundle.of(context).loadString("assets/data.json") );
But I get the error:
The argument type 'Future< String>' can't be assigned to the parameter type 'String'.
First import dart:convert . You can parse data from a JSON file as follows: Future<void> readJson() async { final String response = await rootBundle. loadString('assets/sample.
Try out :
String data = await DefaultAssetBundle.of(context).loadString("assets/data.json"); final jsonResult = jsonDecode(data); //latest Dart
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With