【问题标题】:Error '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable<dynamic>'错误“_InternalLinkedHashMap<String, dynamic>”不是“Iterable<dynamic>”类型的子类型
【发布时间】:2019-01-03 01:21:10
【问题描述】:

使用搜索功能显示从第三个第三方 API 获取的 A-List 该错误仅在我运行应用程序时显示,它说 _InternalLinkedHashMap' 不是“Iterable”类型的子类型

请帮忙 *** 使用此错误显示的代码编辑后显示的新错误 type 'String' 不是 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)

【问题讨论】:

    标签: flutter dart fluttermap


    【解决方案1】:

    responseJson 的值是一个映射。您正在尝试对其进行 for ... in 处理,这仅适用于可迭代对象,而地图不是可迭代对象。

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

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

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

    (我对 Flutter 了解得不够多,不知道对状态进行异步初始化是否是一种好习惯,但我认为这很危险 - 例如,小部件可能在调用 setState 之前被销毁) .

    【讨论】:

    • { "ID": 0, "BusinessUnitID": 1, "CompanyCode": "Code", "Number": 419, "Name": "Some Name", "ShortName": "ShortName ", "City": "City City" }, JSON格式是这样的,我只想检索listing和搜索功能的city和businessunitid
    • 刚刚通过这篇文章了解了Json的不同结构medium.com/flutter-community/…感谢回答
    • 而我拥有的 Json 不是地图列表
    【解决方案2】:

    我正在使用 php,这是它的工作示例,请记住 'json_encode($udresponse['userdetails']); 返回您需要的适当数组格式,并在 array_push($udresponse['userdetails'],$userdetails); 之前将每个项目添加到数组中。

    $udresponse["userdetails"] = array();
    $query = mysqli_query($db->connect(),"SELECT * FROM employee WHERE  Employee_Id ='$UserId'") or die(mysqli_error());
    if (mysqli_num_rows($query) > 0) {
        while ($row = mysqli_fetch_array($query)) {
            $userdetails= array();
            // user details
            $userdetails["customerid"]=$row["Employee_Id"];
            $userid=$row["Employee_Id"];
            $userdetails["username"]=$UserName;
            $userdetails["fullname"]=$Fullname;
            $userdetails["email"]=$Email;
            $userdetails["phone"]=$Phone;
            $userdetails["role"]=$UserRole;
            $userdetails["restaurantid"]=$RestaurantId;
            array_push($udresponse['userdetails'],$userdetails);
        }  
        $udresponse["success"] = 1;
        $udresponse["message"] = "sign in Successful";
        //echo json_encode($response);
        echo json_encode($udresponse['userdetails']);
    } else {   
        $udresponse["success"] = 2;
        $udresponse["message"] = "error retrieving employee details";
        echo json_encode($udresponse);
    }
    

    【讨论】:

    猜你喜欢
    • 2021-06-19
    • 2020-11-25
    • 2021-11-06
    • 2019-03-14
    • 2021-04-07
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 2022-07-22
    相关资源
    最近更新 更多