简体   繁体   English

来自 firebase 的 Listview.builder

[英]Listview.builder from firebase

I have this code here which supposedly builds a list from a document in firebase, which ultimately fails as it always goes to return loading .我这里有这段代码,它应该从 firebase 中的文档构建一个列表,最终失败,因为它总是return loading From what I know, it has something to do with it being a future and I think I am accessing it wrongly.据我所知,它与未来有关,我认为我错误地访问了它。 I have tried getting the output as Text and it works, but as aa listview, it does not.我已经尝试将 output 作为文本获取并且它有效,但作为 aa 列表视图,它没有。

I also tried making a function with async on it but the app still outputs loading.我还尝试制作一个带有异步功能的 function,但该应用程序仍会输出加载。 Any help would be appreciated.任何帮助,将不胜感激。

Widget showFriend() {
CollectionReference users = FirebaseFirestore.instance.collection('todos');
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>;
        List<dynamic> fren = [];

        void waitList() async {
          List<dynamic> temp;
          temp = await (data['friends']);
          fren = temp;
        }

        waitList();

        fren = List.from(data['friends']);
        print(fren);
        if (fren.length > 0) {
          ListView.builder(
              itemCount: fren.length,
              itemBuilder: (context, index) {
                return ListTile(title: Text('${fren[index]}'));
              });
        }
      }
      return Text("loading");
    });
}

You are missing return before ListView您在ListView之前缺少return

if (fren.length > 0) {
   return ListView.builder( //here

try the following:尝试以下操作:

  Widget showFriend() {
  CollectionReference users =
      FirebaseFirestore.instance.collection('todos');
  // ignore: newline-before-return
  return FutureBuilder<DocumentSnapshot>(
    // future: users.doc(documentId).get(),
    // ignore: prefer-trailing-comma
    builder:
        (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
      if (snapshot.hasError) {
        return Text("Something went wrong");
      }

      if (snapshot.connectionState == ConnectionState.done) {
        if (snapshot.hasData) {
          if (!snapshot.data!.exists) {
            return Text("Document does not exist");
          } else {
            Map<String, dynamic> data =
                snapshot.data!.data() as Map<String, dynamic>;
            List<dynamic> fren = [];

            List<dynamic> temp;
            temp = data['friends'];
            fren = temp;

            fren = List.from(data['friends']);
            print(fren);
            if (fren.length > 0) {
              ListView.builder(
                  itemCount: fren.length,
                  itemBuilder: (context, index) {
                    return ListTile(title: Text('${fren[index]}'));
                  });
            }
          }
        }
      }
   return Text("loading");
    },
  );
}

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

相关问题 FutureBuilder 和 ListView.builder - FutureBuilder and ListView.builder 如何从数据库中获取数据并将其放入 listview.builder - How to get data from database and into listview.builder ListView.Builder 在 2 个 StreamBuilder 下使用 - flutter - ListView.Builder use under 2 StreamBuilders - flutter 为什么我的 ListView.builder() 不可滚动? - Why is My ListView.builder() is NOT scrollable? Flutter - 如何从具有嵌套对象的数据构建 ListView.builder? - Flutter - How to build a ListView.builder from data that has nested objects? 如何在 Flutter 中手动分配 Listview.builder 的第一个元素? - How to manually assign the first element of a Listview.builder in Flutter? 执行 ListView.builder() 时这段代码有什么问题吗 - Is there any problem in this code while implementing ListView.builder() 如何使用实时数据库“Flutter”中的 id 从根 ref 的其他子项查询和显示 listview.builder 中的数据? - How to query and display data in listview.builder from other child of the root ref, using id in realtime database "Flutter"? 如何在 flutter 中点击包裹在 listview.builder 中的卡片时更改卡片的边框颜色? - How to change the border colour of a card upon tapping it wrapped in listview.builder in flutter? 在 ListView 中从 Firebase 数据库中获取子数据 - Fetch children data from Firebase Database in ListView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM