0

I am trying to get list of data from my firebase database to display on the activity, but I am getting the following error the getter snapshot is not defined for the type object, I try to add a null theck but it still give me the red line I comment aside the line which give the error.

Widget loadPeople(DatabaseReference _peopleRef) {

return StreamBuilder(

  stream: _peopleRef.onValue,

  builder: (context,snapshot){

    if(snapshot.hasData){

      List<UserModel> userModels =<UserModel>[];
      Map<dynamic,dynamic> values = snapshot.data!.snapshot.value; //error come from this line on the second snapshot it gives the red line

      values.forEach((key, value) {
        if(key != FirebaseAuth.FirebaseAuth.instance.currentUser.uid){

          var userModel = UserModel.fromJson(json.decode(json.encode(value)));
          userModel.uid = key;

          userModels.add(userModel);

        }
      });

      return ListView.builder(

          itemCount: userModels.length,
          itemBuilder: (context , index){
            return GestureDetector(

              onTap: () {},

              child: Column(
                children: [
                  ListTile(
                    leading: CircleAvatar(backgroundColor: Colors.primaries[
                    Random().nextInt(Colors.primaries.length)
                     ],
                      child: Text('${userModels[index].firstName.substring(0,1)}',
                          style: TextStyle(color: Colors.white) ),),
                    title: Text('${userModels[index].firstName} ${userModels[index].lastName}',
                      style: TextStyle(color: Colors.white),),
                    subtitle: Text('${userModels[index].phone} ',
                      style: TextStyle(color: Colors.white),),

                  ),
                  Divider(thickness: 2,)

                ],
              ),
            );
          });
    }

    else return Center (child: CircularProgressIndicator(),);
  });
  • 1
    If you print snapshot.data what are you getting? I think you probably need to write snapshot.data.value – Midhun MP May 12 '21 at 11:05
  • when I try to print snapshot.data I get the error `a value of type object cant be assigned to a variable of type Map` – The greatest May 12 '21 at 11:10

0 Answers0