如何将Firestore的收集数据存储到列表中? [英] How to store data of collection from firestore into a list?

查看:110
本文介绍了如何将Firestore的收集数据存储到列表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将来自firestore的数据存储到一个包含所有文档数据的列表中。

I am looking to store fetch data from firestore into a List which would contain data from all of its documents.

我将列表定义为:

List retrievedData = List();

下一步,按下按钮,我想在特定集合的所有文档中打印数据。因此,我这样做了:

next, on press of button, I wanted to print data in all documents of a specific collection. So, I did this:

RaisedButton(
        onPressed: () async {

          var collectionReferece = await Firestore.instance.collection('insults');
          collectionReferece.getDocuments().then((collectionSnapshot){
            retrievedData = collectionSnapshot.documents.toList();
          });

          print(retrievedData);
        },

我希望在控制台中看到它:

I am expecting this in console:

I/flutter (11351): [{index: 200, title: This is a test 1},{index: 100, title: This is a test 2}]

但是我明白了:

I/flutter (11351): [Instance of 'DocumentSnapshot', Instance of 'DocumentSnapshot']

此外,我只想将此数据存储在列表或任何其他变量中,请帮帮我。谢谢。

Also, I just want to store this data in a list or any other variable. Help me out. Thank you.

编辑:

我尝试使用 forEach ,但是不断增加前夕按下按钮。

I tried to use forEach but it keeps on adding on every press of button.

推荐答案

如果要:


  1. 从firestore中检索数据

  2. 添加到列表

  3. 创建listview.builder

然后您可以执行以下操作,首先在您的 State 类下声明以下变量:

Then you can do the following, first declare the following variables under your State class:

class _MyHomePageState extends State<MyHomePage> {
  bool isFirstTime = false;
  List<DocumentSnapshot> datas = List<DocumentSnapshot>();

接下来,创建一个名为 getData()将在 onPressed 中引用:

Next, create a method called getData() which will be referenced in onPressed:

      floatingActionButton: FloatingActionButton(
        onPressed: getData,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), 

  getData() async {
    if (!isFirstTime) {
      QuerySnapshot snap =
          await Firestore.instance.collection("insults").getDocuments();
      isFirstTime = true;
      setState(() {
        datas.addAll(snap.documents);
      });
    }
  }

在此按一下FAB,您将获得结果集合中的数据。我们使用布尔值每次点击仅检索一次。在您覆盖的方法处置中:

Here on press of the FAB, you will get the data inside the insults collection. We use the boolean to only retrieve once per click. Inside the method dispose which you override:

  @override
  void dispose() {
    super.dispose();
    this.isFirstTime = false;
  }
}

您可以分配 isFirstTime 再次设为 false 。然后要显示数据,可以使用 AppBar 的属性 body ,将其分配给 Center 小部件,而 Center 小部件将包含 listview

You can assign isFirstTime to false again. Then to display the data, You can use the property body of AppBar, assign it to Center widget, and the Center widget will contain the listview:

      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: ListView.builder(
          itemCount: datas.length,
          itemBuilder: (context, index) {
            return ListTile(
              title: Text('${datas[index]["index"]}'),
              subtitle: Text('${datas[index]["title"]}'),
            );
          },
        ),

使用 listview.builder ,您将在屏幕上看到一个列表,而不必使用 forEach 来迭代列表,而只需使用 get 运算符 [] 可以在li中获取数据

Using listview.builder, you will have a list in your screen and you dont have to use forEach to iterate a list. You just have to use the get operator [] to be able to get the data inside the list.

这篇关于如何将Firestore的收集数据存储到列表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆