Home > other >  Flutter Bad state: cannot get a field on a DocumentSnapshotPlatform which does not exist
Flutter Bad state: cannot get a field on a DocumentSnapshotPlatform which does not exist

Time:07-21

Im getting this error Bad state: cannot get a field on a DocumentSnapshotPlatform which does not exist , when i want to show current user name and email from firebase

  • Imported packages

  • import 'package:firebase_auth/firebase_auth.dart';

  • import 'package:flutter/material.dart';

  • import 'package:cloud_firestore/cloud_firestore.dart';

Full code for profile page

class ProfilePage extends StatefulWidget {

const ProfilePage({Key? key}) : super(key: key);

@override
State<ProfilePage> createState() => _ProfilePageState();
}

class _ProfilePageState extends State<ProfilePage> {
String email = '';
String name = '';

Future getCurrentUserData() async{
  try {
    DocumentSnapshot ds = await FirebaseFirestore.instance.collection('users').doc(FirebaseAuth.instance.currentUser!.uid).get();
    String  email = ds.get('email');
    String name = ds.get('name');
    return [email,name];
  }catch(e){
    print(e.toString());
    return null;
  }
}

@override
Widget build(BuildContext context) {
  return Scaffold(
    body: Center(child:
    Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
      Text(name),
      Text(email),
        MaterialButton(
          onPressed: (){
          FirebaseAuth.instance.signOut();
        },
        color: Colors.deepPurple,
          child: const Text('Sign Out', style: TextStyle(color: Colors.white),),
        ),
        MaterialButton(
          onPressed: () async{
          dynamic names = await getCurrentUserData();
          if (names!=null) {
            setState(() {
            email = names[0];
            name = names[1];
            });
          }
        },
        color: Colors.deepPurple,
          child: const Text('DISPLAY DATA', style: TextStyle(color: Colors.white),),
        ),
    ],)),
  );
}
}

enter image description here

Any ideas why ?

CodePudding user response:

Don't use .get() on the DocumentSnapshot! Try this instead:

Future getCurrentUserData() async{
  try {
    final ref = FirebaseFirestore.instance.collection('users').doc(FirebaseAuth.instance.currentUser!.uid);
    final ds = await ref.get();
    final data = ds.data() as Map<String, dynamic>;

    String email = data['email'];
    String name = data['name'];
    return [email,name];
  }catch(e){
    print(e.toString());
    return null;
  }
}

CodePudding user response:

Change your function to:

   Future getCurrentUserData() async{
     try {
       DocumentSnapshot ds = await FirebaseFirestore.instance.collection('users').doc(FirebaseAuth.instance.currentUser!.uid).get();
       final data = ds.data();
       String  email = data!['email'];
       String name = data!['name'];
       return [email,name];
     }catch(e){
       print(e.toString());
       return null;
     }
  }
  • Related