Home > Back-end >  '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable<
'_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable<

Time:04-18

I have a json object which i want to grab and add to the model TestModel.

This is the json:

{
  "fontSize" : "39",
  "fontFamily" : "A",
  "fontWeigth" : "bold",
  "fontColor" : "blue"
}

The following model is used :

class TestModel {
  late String fontSize;

  TestModel(this.fontSize);

  TestModel.fromJson(Map<String, dynamic> json) {
    fontSize = (json['fontSize']);
  }
}

The class DataFetcher() has a method fetchData() where i want to fetch the json data and then add it to the model TestModel().

class DataFetcher{
  Future<List<TestModel>> fetchData() async {
    var settingsList = List<TestModel>.empty();
    String jsonData = await rootBundle.loadString('assets/test_settings.json');

    var data = jsonDecode(jsonData);
    debugPrint(jsonEncode(data).toString());
    for (var value in data) {
      settingsList.add(TestModel.fromJson(value));
    }

    return settingsList;
  }
}


But i get the following error when i want test if this method works:

I/flutter (12015): {fontSize: 39, fontFamily: A, fontWeigth: bold, fontColor: blue}
E/flutter (12015): [ERROR:flutter/shell/common/shell.cc(93)] Dart Unhandled Exception: 
type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable<dynamic>', stack trace: #0     
 FetchSettings.getSettingsJson (package:test_project/controllers/getters/get_test.dart:15:23)
E/flutter (12015): <asynchronous suspension>
E/flutter (12015): 

data is of type _InternalLinkedHashMap<String, dynamic> and the for loop expects type Iterable<dynmic>

As you can see when i debug data i get the object back with type Map<String, dynamic>

How can resolve this error?

Edit

I have changed the content of json file

CodePudding user response:

if its just one item, you should do this.

class DataFetcher {
  Future<List<TestModel>> fetchData() async {
    List<TestModel> settingsList = [];
    String jsonData = await rootBundle.loadString('assets/test_settings.json');
//its just one item in the json file, so just show that item instead of trying to iterate through it
    var aa  = TestModel.fromJson(jsonDecode(jsonData));

    print(aa.fontSize);
    
    return settingsList;
  }
}

but if for example, your json is multiple.. like this..

    [
  {
    "fontSize" : "39",
    "Somethingelse": "asdasasd"
  }
,
  {
    "fontSize" : "10",
    "Somethingelse": "Heasdasdasdllo"
  }
,
  {
    "fontSize" : "25",
    "Somethingelse": "aaaaaaaaaaaaaaaaaaaa"
  }

]

then you have to do this..

class DataFetcher {
  Future<List<TestModel>> fetchData() async {
    List<TestModel> settingsList = [];
    String jsonData = await rootBundle.loadString('assets/test_settings.json');
    var aa  = jsonDecode(jsonData) as List;
    for (var value in aa) {
      settingsList.add(TestModel.fromJson(value));
    }
    return settingsList;
  }
}
  • Related