Error '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable<dynamic>'

user10040349 picture user10040349 · Jul 26, 2018 · Viewed 12k times · Source

Displaying A-List fetched from a 3rd a Third Party API with the search Function the error only shows when I ran the App, It Says _InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable

Please Welp *** Edit A new Error Showed after playing with the code this error showed type 'String' is not a subtype of type Map-dynamic, dynamic-

        Future<Null> getStoreDetails() async {
                var basicAuth = 'Basic ' +
                    base64Encode(utf8.encode('api_token_key'));
                var result;
             
                var response = await http.get(url, headers: {'authorization': basicAuth});
                if (response.statusCode == 200) {
                  var responseJson = json.decode(response.body);
                  setState(() {
             /Where the error is 
                    for (Map storedetails in responseJson) {
                      _searchResult.add(StoreDetails.fromJson(storedetails));
                    }
                  });
                } else if (response.statusCode != 200) {
                  result = "Error getting response:\nHttp status ${response.statusCode}";
                  print(result);
                }
              }
              @override
              void initState() {
                super.initState();
                getStoreDetails();
              }

Data Model Class

class StoreDetails {
  final int businessunitid;
  String citydescription;

  StoreDetails({
    this.businessunitid,
    this.citydescription,
  });

   factory StoreDetails.fromJson(Map<String, dynamic> data) {
    return new StoreDetails(
      businessunitid: data['businessunitid'],
      citydescription: data['citydescription'],
      
    );
  }
}

The Error

E/flutter ( 3566): type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable<dynamic>'
E/flutter ( 3566): #0      SearchStoreState.getStoreDetails.<anonymous closure> (package:rsc_prototype/screens/searchstore_screen.dart:43:34)
E/flutter ( 3566): #1      State.setState (package:flutter/src/widgets/framework.dart:1125:30)
E/flutter ( 3566): #2      SearchStoreState.getStoreDetails (package:rsc_prototype/screens/searchstore_screen.dart:42:7)
E/flutter ( 3566): <asynchronous suspension>
E/flutter ( 3566): #3      SearchStoreState.initState (package:rsc_prototype/screens/searchstore_screen.dart:56:5)
E/flutter ( 3566): #4      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3751:58)

Answer

lrn picture lrn · Jul 26, 2018

The value of responseJson is a map. You are trying to do a for ... in on it, and that only works with iterables, and maps are not iterables.

You need to figure out the structure of the JSON you receive. It might contain a list that you want to iterate, or you might want to iterate responseJson.values. It's impossible to know without knowing the format and what you want to do with it.

If, as you say in a comment below, the JSON is a single object, then your code should probably just be:

...
setState(() {
  _searchResult.add(StoreDetails.fromJson(responseJson));
});
...

(I don't know enough about Flutter to know whether it's good practice to have an asynchronous initialization of the state, but I expect it to be dangerous - e.g., the widget might get destroyed before the setState is called).