【问题标题】:type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<Map<String, dynamic>>'type '_InternalLinkedHashMap<String, dynamic>' 不是类型 'List<Map<String, dynamic>>' 的子类型
【发布时间】:2019-05-02 07:08:28
【问题描述】:

我在某些情况下有点卡住了。

1) type '_InternalLinkedHashMap&lt;String, dynamic&gt;' is not a subtype of type 'List&lt;Map&lt;String, dynamic&gt;&gt;'

我的代码。

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 小时,但没有得到任何结果。我哪里错了?引导我。

【问题讨论】:

  • 尝试阅读错误信息。 _InternalLinkedHashMapMap 相同(只是内部表示)。该值是Map&lt;String,dynamic&gt;,您尝试分配的类型是List&lt;Map&lt;String,dynamic&gt;&gt;。所以你期望一个列表,但实际值不是一个列表,它是一个单一的值。
  • @GünterZöchbauer 抱歉,我是 Flutter 的新手。我知道无法投射列表,但我该如何解决。意味着 Map 有键 ..name 是“数据”,而这个键有 List> (字典数组)。应该怎么写?
  • 所以不要声明 List>。我必须声明 var arrayOfProductList; ?
  • 您能否发布带有简单工作 api 调用的单页应用演示?
  • 尝试使用没有泛型定义的列表 > 只需删除它,如果这解决了问题,请告诉我

标签: dart flutter


【解决方案1】:

dicOfData["data"] as List&lt;Map&lt;String, dynamic&gt;&gt; 的强制转换尝试似乎导致了您收到的类型不匹配错误。如果您可以提供有关快照包含的数据的更多详细信息,这可以帮助我们确定导致错误的原因。

【讨论】:

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