【问题标题】:How to retreive data from firestore flutter如何从 Firestore Flutter 中检索数据
【发布时间】:2021-06-30 02:25:29
【问题描述】:

我是 Flutter 和 firebase 集成的新手,在从 firebase 集合中检索所有数据时遇到了一些麻烦。

这个方法我试过了:

    getCollection() {
      CollectionReference coleccion =
          FirebaseFirestore.instance.collection('materias');
      return Container(
        child: StreamBuilder(
          stream: coleccion.doc('aprobadas').snapshots(),
          builder: (BuildContext context, AsyncSnapshot snapshot) {
            if (snapshot.connectionState == ConnectionState.active) {
              return Text(snapshot.data.data()['codigo'],
                  style: TextStyle(fontSize: 50, color: Colors.white));
            } else {
              return CircularProgressIndicator();
            }
          },
        ),
      );
    }

现在我有点沮丧,因为我尝试了不同的方法但不起作用。

非常感谢所有帮助。

最好的问候

【问题讨论】:

  • 你得到什么错误/你用这个方法遇到什么问题?
  • 快照数据为空,循环进度指示器卡住,我在firebase控制台查看数据,有需要显示的数据
  • 如果你使用snapshot.connectionState == ConnectionState,done会发生什么?

标签: firebase flutter google-cloud-firestore


【解决方案1】:

可以使用以下代码从 firestore 检索数据到 flutter。

一次性阅读

调用Query.getDocumentReference.get 方法

class GetUserName extends StatelessWidget {
  final String documentId;

  GetUserName(this.documentId);

  @override
  Widget build(BuildContext context) {
    CollectionReference users = FirebaseFirestore.instance.collection('users');

    return FutureBuilder<DocumentSnapshot>(
      future: users.doc(documentId).get(),
      builder:
      (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {

        if (snapshot.hasError) {
          return Text("Something went wrong");
        }

        if (snapshot.hasData && !snapshot.data!.exists) {
          return Text("Document does not exist");
        }

        if (snapshot.connectionState == ConnectionState.done) {
          Map<String, dynamic> data = snapshot.data!.data() as Map<String, dynamic>;
          return Text("Full Name: ${data['full_name']}         ${data['last_name']}");
        }

        return Text("loading");
      },
    );
  }
}

实时变化

FlutterFire 支持处理集合和文档的实时更改。初始请求时会提供一个新事件,以及每当发生更改(修改、删除或添加)时对集合/文档的任何后续更改。

CollectionReferenceDocumentReference 都提供了一个返回 Stream 的 snapshots() 方法:

Stream collectionStream = FirebaseFirestore.instance.collection('users').snapshots();
Stream documentStream = FirebaseFirestore.instance.collection('users').doc('ABC123').snapshots();

请参考官方文档here

【讨论】:

    【解决方案2】:

    您可以使用StreamBuilder。这将很容易理解。

    StreamBuilder(
        stream: FirebaseFirestore.instance.collection("collection").snapshot,
        builder: (BuildContext context,snapshot) {
            if(snapshot.hasdata!=true) {
                return CircularProgressIndicator();
            } else {
                return ListView.builder(
                    itemcount:snapshot.data.docs.length,
                    builder(context,index) {
                    return Text(snapshot.data.docs[index].data()["filedname"]);
            }
        }
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 2021-04-17
      • 1970-01-01
      • 2021-11-28
      • 2021-10-26
      • 2021-10-28
      相关资源
      最近更新 更多