错误'_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).