I am trying to read a lot of data from a JSON file in flutter. The data retrieval results in jank so I decided to use compute
but then it didn't return any results.
This is my code with compute
that does not return data:
Future<List<City>> getCityList (BuildContext context) async{
final response = await DefaultAssetBundle.of(context).loadString('Assets/citylist.json');
final decoded = await compute(json.decode, response);
return decoded.map<City>((json) => fromJson(json)).toList();
}
This is my code without compute that does returns data but results in jank :
Future<List<City>> getCityList (BuildContext context) async{
final response = await DefaultAssetBundle.of(context).loadString('Assets/citylist.json');
final decoded = json.decode(response);
return decoded.map<City>((json) => fromJson(json)).toList();
}
This is the code for my Futurebuilder:
FutureBuilder(
future: getCityList(context),
builder: (context, snapshot) {
if (snapshot.hasData){
return ListView.builder(
itemCount: snapshot.data?.length ?? 0,
itemBuilder: (BuildContext context, int index) {
return FlatButton(
onPressed: (){
},
child: Container(
child: Text(snapshot.data[index].name, style: TextStyle(fontSize: 20.0, color: Theme.of(context).accentColor),),
padding: const EdgeInsets.all(15.0),
),
);
}
);
}
return Center(child: CircularProgressIndicator(),);
})