【发布时间】:2019-05-02 07:08:28
【问题描述】:
我在某些情况下有点卡住了。
1) type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<Map<String, dynamic>>'
我的代码。
List<Map<String, dynamic>> arrayOfProductList = List<Map<String, dynamic>>();
调用 API
Future<Map<String, dynamic>> _callWebServiceForGetProductList() async{
String strURL = 'http:xxxxxx/productlist.php';
Map<String, String> headerDic = {"Authorization": "ff624bc580ab48a3910d4352dxxx712"};
Map<String, String> paramDic = {"page": "1", "search": "", "term_id": ""};
http.Response httpRes = await http.post(strURL, headers: headerDic, body: paramDic);
Map<String, dynamic> responseDic = json.decode(httpRes.body);
return responseDic;
}
FutureBuilder 小部件和东西
Expanded(
child: Container(
child: FutureBuilder(
future: _callWebServiceForGetProductList(),
builder: (BuildContext context, AsyncSnapshot snap) {
if(snap.hasData) {
Map<String, dynamic> dicOfData = snap.data;
arrayOfProductList = dicOfData["data"] as List<Map<String, dynamic>>;
print(arrayOfProductList);
_setupProductListView();
}
else if(snap.hasError) {
showDialog(context: context, builder:(_) => AlertDialog(
content: Text("Error ${snap.hasError.toString()}"),
title: Text("Error"),
));
}
return Center(child: CircularProgressIndicator());
}
),
),
)
我在谷歌上搜索了过去 2 小时,但没有得到任何结果。我哪里错了?引导我。
【问题讨论】:
-
尝试阅读错误信息。
_InternalLinkedHashMap与Map相同(只是内部表示)。该值是Map<String,dynamic>,您尝试分配的类型是List<Map<String,dynamic>>。所以你期望一个列表,但实际值不是一个列表,它是一个单一的值。 -
@GünterZöchbauer 抱歉,我是 Flutter 的新手。我知道无法投射列表,但我该如何解决。意味着 Map
有键 ..name 是“数据”,而这个键有 List -
所以不要声明 List
-
您能否发布带有简单工作 api 调用的单页应用演示?
-
尝试使用没有泛型定义的列表