I want to access my document field uname
from cloud firestore . I made the uid and document id same and when I tried to access the document field it shows the error Bad state: field does not exist within the DocumentSnapshotPlatform
This is my code
class test extends StatefulWidget {
const test({Key? key}) : super(key: key);
_testState createState() => _testState();
}
class _testState extends State<test> {
final userData = FirebaseFirestore.instance
.collection("Users")
.doc(FirebaseAuth.instance.currentUser!.uid)
.get()
.then((value) => print((value.data() ? ["uname"])));
@override
Widget build(BuildContext context) {
return Scaffold(
body: Text(()),
);
}
2
Answers
the return of
get()
is actually aDocumentSnapshot
, you need to access thedata()
to get theMap<String, dynamic>
of your document’s fields, then access the"uname"
value from it like this:You’ll have to
get
the data of theDocumentSnapshot
usingdata()
and then access theuname
.Try to replace
value
withvalue.data()
And access using uname using:
value.data()?["uname"]