Home > Software design >  Unidentified Name Error while using required.this value
Unidentified Name Error while using required.this value

Time:06-18

So Iam trying to make a chat-app using Firebase-Firestore where you have an option of creating group. Now after creating a group Iam navigating to a screen from where you can add member. But for that I gonna need to pass the groupRoomId.

Here Iam navigating to add member screen:

 Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => Add_Member(
              groupRoomId: groupRoomId,
            )));

Now here is my add member screen:

class Add_Member extends StatefulWidget {
  final String groupRoomId;
  Add_Member({required this.groupRoomId});
  _Add_Member createState() => _Add_Member();
}

class _Add_Member extends State<Add_Member> {
  QuerySnapshot<Map<String, dynamic>>? searchResultSnapshot;
  QuerySnapshot<Map<String, dynamic>>? Snapshot;
  bool isLoading = false;
  bool haveUserSearched = false;
  bool isChecked = false;
  DatabaseMethods databaseMethods = new DatabaseMethods();
  var searchEditingController = TextEditingController();
  final DocumentReference <Map<String, dynamic>>? documentReference = FirebaseFirestore.instance.doc("groups/$groupRoomId");

addremovemember(userName){
    documentReference?.snapshots().listen((datasnapshot){
      if (datasnapshot.data()!.containsValue("$userName")){
        FirebaseFirestore.instance
            .collection('groups')
            .doc('$groupRoomId')
            .update({"users" : FieldValue.arrayRemove(["$userName"])});
      }
      else{
        FirebaseFirestore.instance
            .collection('groups')
            .doc('$groupRoomId')
            .update({"users" : FieldValue.arrayUnion(["$userName"])});
      }
    });
  }
......

So what is happening that in DocumentReference $groupRoomId is showing Unidentified Name Error. My ultimate aim is to being able to add and remove error(in future too) as you may see in addremovemember function. I would highly appreciable if you can help me.

CodePudding user response:

It happens so because you are referencing groupRoomId which is outside the build scope.

For this to work use the keyword widget

Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => Add_Member(
              groupRoomId: widget.groupRoomId,// <-here
            )));

CodePudding user response:

@Davis is right. You are referencing groupRoomId which is outside the build scope.

For this to work, access it using the widget keyword.

Replace .doc('$groupRoomId') with .doc(widget.groupRoomId)

  • Related