未为flutter中的类型对象定义getter快照

时间:2021-05-12 11:01:55

标签: firebase flutter dart firebase-realtime-database

我正在尝试从我的 firebase 数据库中获取数据列表以显示在活动中,但我收到以下错误 getter 快照未为类型对象定义,我尝试添加一个空确认但它仍然给出我是红线,我在给出错误的行旁边注释。

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(),);
  });

0 个答案:

没有答案