How can fix the issue Unhandled Exception: type ‘_InternalLinkedHashMap<String, dynamic>’ is not a subtype of type ‘Iterable’ i am trying to parse data from a json Api in Android Studio flutter (dart) but i keep getting the error above, the app open fine but displays nothing and i get the error in my logcat.
Any suggestion will be greatly appreciated.
//Sample Json//
{
"data": [
{
"title": "Ronix",
"year": "Year 5",
"category": "Admin"
},
{
"title": "Ultra",
"year": "Year 11",
"category": "Tutor"
},
{
"title": "Sonet",
"year": "Year 3",
"category": "Admin"
}
]
}
Here is my class .dart file
class DataClass{
String title ="";
String year="";
String category="";
DataClass(this.title, this.year, this.category);
DataClass.fromJson(Map<String, dynamic> json){
title = json["title"];
year = json["year"];
category = json["category"];
}
}
And my main .dart file
class CreateState1 extends State<JsonData1>{
List<DataClass> _dataClass = [];
Future<List<DataClass>> getMovieData() async {
var response = await http.get(Uri.parse("myjsonApiUrl"));
var datas = [];
if(response.statusCode == 200){
var datasJson = json.decode(response.body);
for(var dataJson in datasJson){
datas.add(DataClass.fromJson(dataJson));
}
}
return _dataClass;
}
@override
Widget build(BuildContext context) {
getMovieData().then((value) {
setState(() {
_dataClass.addAll(value);
});
});
return Scaffold(
backgroundColor: Colors.cyan,
body: ListView.builder(
itemBuilder: (context, index) {
return Card(
child: Column(
children: <Widget>[
Text(_dataClass[index].title),
Text( _dataClass[index].year),
Text(_dataClass[index].category),
],
),
);
},
itemCount: _dataClass.length,
),
);
}
}
3
Answers
In your example,
datasJson
is a map with a "data
" key. You want to iterate over the list assigned to the "data
" key:instead of
use this
as @hacker1024 said , you are missing key there.
if you are getting data from Api, try using FutureBuilder.
I’m loading from assets folder here (dont forget to add on
pubspec.yaml
)And for implementation