skip to Main Content

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

2

Answers


  1. 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");
        },
      );
    }
    
    Login or Signup to reply.
  2. You are missing return before ListView

    if (fren.length > 0) {
       return ListView.builder( //here
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search