How can I resolve " Bad state: cannot get a field on a DocumentSnapshotPlatform which does not exist"

Coding-Jutsu

I have this problem when I try to show the status of the user with his name in my appBar:

      appBar: AppBar(
    title: StreamBuilder<DocumentSnapshot>(
        stream:  _firestore.collection('users').doc(userMap['uid']).snapshots(),
        builder: (context, snapshot){
          if (snapshot.data != null){
            return Container(
              child: Column(
                children: [
                  Text(userMap['name']),
                  Text(
                    snapshot.data!['status'],
                    style: TextStyle(fontSize: 14),
                  )
                ],
              ),
            );
          }else{
            return Container();
          }
        }
    ),
    backgroundColor: Colors.white,
    leading: IconButton(
      icon:Icon(Icons.arrow_back_rounded, color: Colors.black,), onPressed: () {
      Navigator.of(context)
          . pushReplacement(MaterialPageRoute(builder: (_) => chatpage()));},
    ),
  ),

All the process for see the user status work in firebase I think the problem is in the first part but I show the rest of the code.

class _chatpageState extends State<chatpage> with WidgetsBindingObserver{


Map<String,dynamic>? userMap;
bool isLoading = false;
final FirebaseAuth _auth = FirebaseAuth.instance;
final TextEditingController _search = TextEditingController();
final FirebaseFirestore _firestore = FirebaseFirestore.instance;

@override
void initState(){
super.initState();
WidgetsBinding.instance!.addObserver(this);
setStatus("Online");
}
void setStatus(String status)async{

await _firestore.collection('users').doc(_auth.currentUser!.uid).update({
  "status": status
});

}

@override
void didChangeAppLifecycleState(AppLifecycleState state){
if(state == AppLifecycleState.resumed){
  setStatus("Online");
  //online
 }else{
  setStatus("Offline");
  //offline
 }
 } 

And there is a litlle part of my methods:

    await _firestore.collection('users').doc(_auth.currentUser!.uid).set({
  "name": name,
  "email": email,
  "status": "Unavalible",
  "uid": _auth.currentUser!.uid,
});

If somoene can help I will be very greatful ty :)

Peter O.

Change snapshot.data!['status'] to snapshot.data.get('status') in the appBar (if you are sure field 'status' exists).

Or

dynamic data = snapshot.data.data();
print(data['status']);
return Container(
  child: Column(
    children: [
      Text(userMap['name']),
      Text(
        data['status'],
        style: TextStyle(fontSize: 14),
      )
    ],
  ),
);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

HotTag

Archive