简体   繁体   English

为什么当我将数据添加到 firebase 时,数据一直在循环?

[英]Why when i add data to firebase, the data keep looping?

I'm new to flutter and firebase and I do not know why when I upload any data to firebase the data will keep repeating the same thing but when I hot restart the upload item back to 1, this is my code:我是 flutter 和 firebase 的新手,我不知道为什么当我将任何数据上传到 firebase 时,数据会不断重复同样的事情,但是当我热重启上传项目回到 1 时,这是我的代码:

Future getDocId() async {
await FirebaseFirestore.instance
    .collection('users')
    .orderBy('mobile', descending: true)
    .get()
    .then(
      (snapshot) => snapshot.docs.forEach(
        (document) {
          print(document.reference);
          docIDs.add(document.reference.id);
        },
      ),
    );

} }

body: Center(
    child: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Expanded(
          child: FutureBuilder(
            future: getDocId(),
            builder: (context, snapshot) {
              return ListView.builder(
                itemCount: docIDs.length,
                itemBuilder: (context, index) {
                  return Padding(
                    padding: const EdgeInsets.all(10.0),
                    child: ListTile(
                      title: ReadUser(documentId: docIDs[index]),
                      tileColor: Colors.purple[100],
                    ),
                  );
                },
              );
            },
          ),
        ),

This is my future builder这是我未来的建设者

 return FutureBuilder<DocumentSnapshot>(
  future: users.doc(documentId).get(),
  builder: ((context, snapshot) {
    if (snapshot.connectionState == ConnectionState.done) {
      Map<String, dynamic> data =
          snapshot.data!.data() as Map<String, dynamic>;
      return Text('Name:' +
          '${data['name']}' +
          "\n"
              'Email:' +
          '${data['email']}' +
          "\n"
              'Mobile Number:' +
          '+' +
          '${data['mobile']}' +
          "");
    }
    return Text('Loading..');
  }),
);

the way you are fetching your data is wrong, instead of pass the result into outside variable you need to return it like this, I assume docIDs is a list of string:您获取数据的方式是错误的,而不是将结果传递给外部变量,您需要像这样返回它,我假设 docIDs 是一个字符串列表:

Future<List<String>> getDocId() async {
    try {
      var snapshot = await FirebaseFirestore.instance
          .collection('users')
          .orderBy('mobile', descending: true)
          .get();
      return snapshot.docs.map((document) => document.reference.id).toList();
    } catch (e) {
      return [];
    }
  }

then change your FutureBuilder to this:然后将您的FutureBuilder更改为:

FutureBuilder<List<String>>(
  future: getDocId(),
  builder: (context, snapshot) {
    switch (snapshot.connectionState) {
      case ConnectionState.waiting:
        return Text('Loading....');
      default:
        if (snapshot.hasError) {
          return Text('Error: ${snapshot.error}');
        } else {
          List<String> data = snapshot.data ?? [];
          return ListView.builder(
            itemCount: data.length,
            itemBuilder: (context, index) {
              return Padding(
                padding: const EdgeInsets.all(10.0),
                child: ListTile(
                  title: ReadUser(documentId: data[index]),
                  tileColor: Colors.purple[100],
                ),
              );
            },
          );
        }
    }
  },
),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么页面刷新时firebase的实时数据库不加载数据 - Why is firebase's Realtime Database not loading data when page refreshes 如何在firebase数据库中添加数据 - How to add data in firebase database 当我上传数据到firebase时,有时最后一个字符被截断 - When I upload data to firebase, sometimes the last character is cut off 当我尝试在 firebase db 中写入数据时出现异常 - When i trying to wirte data in firebase db take an exception 当我尝试将数据上传到 firebase 时,它没有上传? - When I am trying to upload my data to firebase it is not uploading? 为什么我的 Firebase Firestore 数据未显示在 android 中 - Why my Firebase firestore data is not showing in android 如何使用 flutter 向 firebase 添加新数据? - how to add new data to firebase with flutter? 我刚开始使用 firebase 并在获取数据时遇到 orderBy desc function 问题 - I just started with firebase and have problem with orderBy desc function when getting data 当我添加 firebase ZEAE18BC41E1434DD98FA2DD989531DAZ8 时 javascript function 不工作 - javascript function is not working when i add firebase sdk i 为什么数据未显示来自 ReactJs 中的 firebase 用户的数据? - Why data is not showing getting from firebase user in ReactJs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM