【问题标题】:reading firestore array field on Flutter list builder在 Flutter 列表生成器上读取 Firestore 数组字段
【发布时间】:2021-03-05 08:55:30
【问题描述】:

在 Flutter 列表构建器上读取 Firestore 数组字段。这是我尝试过的代码。 但是我收到了这个错误信息:

请帮忙!

class _ListClients2 extends State<ListClients2> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: FutureBuilder(
      future: getList(),
      builder: (context, AsyncSnapshot<List<dynamic>> snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return CircularProgressIndicator();
        } else {
          return Center(
            child: ListView.builder(
                padding: const EdgeInsets.only(bottom: 20.0),
                scrollDirection: Axis.vertical,
                shrinkWrap: true,
                itemCount: snapshot.data.length,
                itemBuilder: (context, index) {
                  return Center(
                    child: ListTile(
                      title: Text(snapshot.data[index].data
                          .toString()), //snapshot data should dispaly in this text field
                    ),
                  );
                }),
          );
        }
      }
   
  Future<List<dynamic>> getList() async {
    var firestore = Firestore.instance;

    DocumentReference docRef =
        firestore.collection('admin_users').document(idUser);

   
    return docRef.get().then((datasnapshot) {
      if (datasnapshot.exists) {
        List<dynamic> info = datasnapshot.data['clients'].toList();
        print('#');
        print(info); 
        print(info.length); 
        return info;
      }
    });
  }

【问题讨论】:

  • 您的snapshot.data[index] 很可能没有返回任何内容。为了不失败,请使用?.。所以snapshot.data[index]?.data?.toString().
  • 谢谢,坦率地说,但仍然有同样的错误信息

标签: arrays flutter google-cloud-firestore


【解决方案1】:

“NoSuchMethodErrorr: Class”错误大多数时候意味着您正在尝试读取数据类型而不是您发送的数据类型。因此,您似乎期望数据快照的“客户”部分是一个列表,但它显然是一个字符串。

我会建议你仔细检查当前的数据快照文本,可能你有类似的东西

"clients": "something here" 

而不是

"clients": [...]

【讨论】:

    【解决方案2】:

    您必须提供 data()['clients'] 而不是 data['clients'] 才能从文档中获取价值。

    List<dynamic> info = datasnapshot.data()['clients'].toList();
    

    【讨论】:

    • 谢谢,Harsha 的回答,我尝试添加括号,但出现语法错误:(,有什么想法吗?
    • 请大家帮忙?还有没有办法获得 Flutter 和 Firebase 的完整文档?
    • 仍然没有找到任何解决方案我真的被卡住了,有没有办法得到 Flutter 开发团队的支持?
    • 你能用你面临的新错误更新你的问题吗?会尽力帮助你。
    • 你好哈沙,对不起,你说的更新是什么意思,因为我仍然遇到同样的问题,
    猜你喜欢
    • 1970-01-01
    • 2021-11-14
    • 2021-05-24
    • 2021-09-28
    • 1970-01-01
    • 2021-11-25
    • 1970-01-01
    • 2021-03-26
    • 2022-12-13
    相关资源
    最近更新 更多