Raymond Chong
Raymond Chong

Reputation: 79

Flutter Try removing the code, or fixing the code before it so that it can be reached

I have a block of code that I've wanted to try but the start of the code becomes a dead code.

Future createGroupChat(
      String groupName, String userName, memberList, memberIDList) async {
    DocumentReference groupDocRef = await groupCollection.add({
      'groupName': groupName,
      'admin': [userName],
      'members': memberList,
      'groupID': '',
    });

    await groupDocRef.update({
      'groupID': groupDocRef.id,
      'members': FieldValue.arrayUnion([userName])
    });

    DocumentReference userDocRef =
        FirebaseFirestore.instance.collection('users').doc(userID);
    return await userDocRef.update({
      'groups': FieldValue.arrayUnion([groupDocRef.id + '_' + groupName])
    });

    DocumentReference memberDocRef =
        FirebaseFirestore.instance.collection('users').doc(memberIDList);
    return await memberDocRef.update({
      'groups': FieldValue.arrayUnion([groupDocRef.id + '_' + groupName])
    });
  }

The dead code starts at DocumentReference memberDocRef =. If I move the DocumentReference memberDocRef on top of DocumentReference userDocRef the DocumentReference userDocRef will become dead code.

Upvotes: 3

Views: 2910

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80904

Dead code means it can't be reached, when you are returning here return await userDocRef.update({ then the code below it will never be executed, just remove the first return:

    DocumentReference userDocRef =
        FirebaseFirestore.instance.collection('users').doc(userID);
        await userDocRef.update({
      'groups': FieldValue.arrayUnion([groupDocRef.id + '_' + groupName])
    });

    DocumentReference memberDocRef =
        FirebaseFirestore.instance.collection('users').doc(memberIDList);
    return await memberDocRef.update({
      'groups': FieldValue.arrayUnion([groupDocRef.id + '_' + groupName])
    });

Upvotes: 2

Related Questions