Home > Back-end >  getting an error in console type 'Null' is not a subtype of type 'UserModel' in
getting an error in console type 'Null' is not a subtype of type 'UserModel' in

Time:08-27

I am trying to make a basic app...its working well but I just noticed that console showing an error

type 'Null' is not a subtype of type 'UserModel' in type cast

here is my code, what's my mistake...

I have commented where I doubt of error but what should I correct...

here is my function for getttinguser model by passing id

Future<UserModel?> getuserdatabyid(String uid) async
{
  UserModel? usermodel;

  final DocumentSnapshot data=await FirebaseFirestore.instance.collection("users").doc(uid).get();

  if(data.data()!=null)
  usermodel=UserModel.fromjson(data.data() as Map<String,dynamic>);

  return usermodel;


}


SafeArea(
          child: Container(
        child: StreamBuilder(
          stream: FirebaseFirestore.instance
              .collection("chatrooms")
              .where("participants.${widget.usermodel.uid}", isEqualTo: true)
              .snapshots(),
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.active) {
              if (snapshot.hasData) {
                QuerySnapshot querysnapshot = snapshot.data as QuerySnapshot;

                return ListView.builder(
                    itemCount: querysnapshot.docs.length,
                    itemBuilder: (context, index) {
                      ChatRoomModel chatroommodel = ChatRoomModel.fromMap(
                          querysnapshot.docs[index].data()
                              as Map<String, dynamic>);
                      Map<String, dynamic> parties =
                          chatroommodel.participants!;
                      List<String> listofpartieskey = parties.keys.toList();

                      listofpartieskey.remove(widget.usermodel.uid);
//todo futurebuilder to be studied
                      return FutureBuilder(
                          future: getuserdatabyid(listofpartieskey[0]),
                          builder: (context, userdata) {
                            UserModel user = userdata.data as UserModel;
//loooks like this error due to this code...

                            return ListTile(
                              leading: CircleAvatar(
                                backgroundImage:
                                    NetworkImage(user.imageurl.toString()),
                              ),
                              title: Text(user.fullname.toString()),
                              subtitle:
                                  Text(chatroommodel.lastMessage.toString()),
                            );
                          });
                    });
              } else {
                if (snapshot.hasError) {
                  return Center(
                    child: Text('Error...'),
                  );
                } else {
                  return Center(
                    child: Text('No data'),
                  );
                }
              }
            } else {
              return Center(
                child: CircularProgressIndicator(),
              );
            }
          },
        ),
      ))

CodePudding user response:

Your future method getuserdatabyid returns nullable user, You can

  builder: (context, userdata) {
 if(userdata.data==null)   return Text("Got null user");
 UserModel user = userdata.data as UserModel!;
  • Related