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

12,615

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

Share:
12,615

Related videos on Youtube

user10040349
Author by

user10040349

Updated on June 04, 2022

Comments

  • user10040349
    user10040349 almost 2 years

    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)
    
  • user10040349
    user10040349 almost 6 years
    { "ID": 0, "BusinessUnitID": 1, "CompanyCode": "Code", "Number": 419, "Name": "Some Name", "ShortName": "ShortName", "City": "City City" }, The JSON format is like this and I only want to retrieve is the city and the businessunitid for the listing and search function
  • user10040349
    user10040349 almost 6 years
    just learned the different structures of the Json through this article medium.com/flutter-community/… Thanks for answering <3
  • user10040349
    user10040349 almost 6 years
    and the Json I have is not a list of maps
  • Dharman
    Dharman over 4 years
    You have an error. mysqli_error() needs one argument. Please consider switching error mode on instead. How to get the error message in MySQLi?
  • Abdul razak
    Abdul razak over 4 years
    That was good programming practice thanks AbdelAziz AbdelLatef.