错误'_InternalLinkedHashMap<字符串,动态>'不是'Iterable&dynamic;'类型的子类型 [英] Error '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable<dynamic>'

查看:199
本文介绍了错误'_InternalLinkedHashMap<字符串,动态>'不是'Iterable&dynamic;'类型的子类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用搜索功能显示从第三者的第三方API获取的列表 该错误仅在我运行该应用程序时显示,它表示_InternalLinkedHashMap'不是'Iterable

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' is not a subtype of type 'Iterable

请哭 ***编辑一个新的错误,并在显示该错误显示的代码后显示 'String'类型不是Map-dynamic,dynamic-

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();
              }

数据模型类

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'],

    );
  }
}

错误

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)

推荐答案

responseJson的值是一个映射.您正在尝试在其上执行for ... in,并且仅适用于可迭代对象,而映射不是可迭代对象.

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.

您需要弄清楚接收到的JSON的结构.它可能包含您要迭代的列表,或者您可能要迭代responseJson.values.不知道格式和要使用的格式是不可能知道的.

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.

如果,正如您在下面的评论中说的那样,JSON是单个对象,那么您的代码可能应该只是:

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

(我对Flutter的了解还不够,不知道对状态进行异步初始化是否是一个好习惯,但是我希望它很危险-例如,在调用setState之前小部件可能会被破坏)

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

这篇关于错误'_InternalLinkedHashMap&lt;字符串,动态&gt;'不是'Iterable&dynamic;'类型的子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆