Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load JSON assets into a Flutter App?

Tags:

flutter

dart

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'.

like image 826
nalyd88 Avatar asked Apr 10 '18 15:04

nalyd88


People also ask

How do I import JSON data into DART?

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.


1 Answers

Try out :

String data = await DefaultAssetBundle.of(context).loadString("assets/data.json"); final jsonResult = jsonDecode(data); //latest Dart 
like image 67
Alexandre Beaudet Avatar answered Sep 22 '22 08:09

Alexandre Beaudet