Home > other >  Flutter Firestore chat not working, display gives errors
Flutter Firestore chat not working, display gives errors

Time:03-21

I have been implementing chat for Flutter through Firestore, and am unable to display the main page itself and the chat groups. The following is the method to get the user groups:

getUserGroups() async {
    return FirebaseFirestore.instance.collection("users").doc(uid).snapshots();
  }

This is the method to display the groups:

_getUserAuthAndJoinedGroups() async {
    _username = (await storage.read(key: 'username')).toString();
    if (mounted) setState(() {});
    DatabaseService(_username).getUserGroups().then((snapshots) {
      // print(snapshots);
      setState(() {
        _groups = snapshots;
      });
    });
  }

Widget groupsList() {
    return StreamBuilder(
      stream: _groups,
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          var data = snapshot.data;
          if (data['groups'] != null) {
            // print(snapshot.data['groups'].length);
            if (snapshot.data['groups'].length != 0) {
              return ListView.builder(
                  itemCount: snapshot.data['groups'].length,
                  shrinkWrap: true,
                  itemBuilder: (context, index) {
                    int reqIndex = snapshot.data['groups'].length - index - 1;
                    return GroupTile(
                        snapshot.data['fullName'],
                        _destructureId(snapshot.data['groups'][reqIndex]),
                        _destructureName(snapshot.data['groups'][reqIndex]));
                  });
            } else {
              return noGroupWidget();
            }
          } else {
            return noGroupWidget();
          }
        } else {
          return Center(child: CircularProgressIndicator());
        }
      },
    );
  }

For the above code snippet, data['groups'] gives me the error:

Object? data The method '[]' can't be unconditionally invoked because the receiver can be 'null'. Try making the call conditional (using '?.') or adding a null check to the target ('!').

Lastly, this declaration gives me an error: Non-nullable instance field '_groups' must be initialized.

Stream _groups;

I have used the 'late' modifier, '?' and '!', but it isn't resolved.

CodePudding user response:

How about you change all snapshot.data['groups'] after var data = snapshot.data; into data['groups']?

Because you are wrapping with condition if (data['groups'] != null), it should not return message that data['groups'] might be null after that condition, if all snapshot.data['groups'] after var data = snapshot.data; into data['groups'].

AFTER

if (snapshot.hasData) {
          var data = snapshot.data;
          if (data['groups'] != null) {
            // print(snapshot.data['groups'].length);
            if (data['groups'].length != 0) {
              return ListView.builder(
                  itemCount: data['groups'].length,
                  shrinkWrap: true,
                  itemBuilder: (context, index) {
                    int reqIndex = data['groups'].length - index - 1;
                    return GroupTile(
                        snapshot.data['fullName'],
                        _destructureId(data['groups'][reqIndex]),
                        _destructureName(data['groups'][reqIndex]));
                  });
            } else {
              return noGroupWidget();
            }
          }

CodePudding user response:

Check upper answer and check firebase rules too.

  • Related