Home > Enterprise >  Listview.builder from firebase
Listview.builder from firebase

Time:11-20

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. 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 a a listview, it does not.

I also tried making a function with async on it but the app still outputs loading. 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");
    });
}

CodePudding user response:

You are missing return before ListView

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

CodePudding user response:

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");
    },
  );
}
  • Related