错误“_InternalLinkedHashMap<String, dynamic>”不是类型 'Iterable<dynamic>' 的子类型

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

显示从第 3 个第三方 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

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,这仅适用于可迭代对象,而地图不是可迭代对象。

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

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

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

(我对 Flutter 的了解还不够,不知道对状态进行异步初始化是否是一种好的做法,但我认为这很危险——例如,小部件可能会在 setState 被调用)。

我正在使用 php 这是它的工作示例,请记住此 'json_encode($udresponse['userdetails']); return 您需要的适当数组格式,并在 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);
}

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

我认为您正在尝试将地图数据设置为列表。 地图数据来自后端,您正在尝试将其设置在列表中。

您可以通过从后端而不是 Map 返回数组来简单地修复它。