【问题标题】:Flutter Error : type 'String' is not a subtype of type 'List<dynamic>' in type castFlutter 错误:类型“String”不是类型转换中“List<dynamic>”类型的子类型
【发布时间】:2021-05-08 20:49:18
【问题描述】:

我想从 firestore 获取图像数组,但出现以下错误:

我的错误: “String”类型不是类型转换中“List”类型的子类型

代码:

Widget build(BuildContext context) {
return Scaffold(
  body: StreamBuilder<QuerySnapshot>(
      stream: _store.load_item(),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          List<Item> _item = [];
          for (var doc in snapshot.data.docs) {
            var data = doc.data();

            _item.add(Item(
                admin_id: data['admin_id'],
                item_id: data['item_id'],
                item_description: data['item_description'],
                item_price: data['item_price'],
                images: List.from(data['images'])));
          }
          return GridView.builder(
            itemCount: _item.length,
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 2),
            itemBuilder: (context, index) {
              return Stack(children: [
                Image(image: NetworkImage(_item[index].images.toString()))
              ]);
            },
          );
        } else {
          return Center(child: Text("Loading....."));
        }
      }),
);

}

【问题讨论】:

    标签: firebase flutter google-cloud-firestore


    【解决方案1】:

    这是导致您的问题的原因:

    images: List.from(data['images'])));
    

    你收到的不是一个列表,而是一个字符串。确保它是 firebase 中的列表。

    【讨论】:

    • 如果确实是列表,那就对了。 print('data['images']) 的输出是什么?
    猜你喜欢
    • 2020-11-24
    • 2022-12-02
    • 1970-01-01
    • 1970-01-01
    • 2019-09-19
    • 1970-01-01
    • 2021-05-10
    • 2021-10-20
    • 2021-04-06
    相关资源
    最近更新 更多