I am new to flutter and firebase, so please forgive me if this is a silly question.
I am trying to read a document from Cloud Firestore and place the data in a defined object called Profile.
This is my current implementation:
final docRef = FirebaseFirestore.instance
.collection('/user_data')
.doc(FirebaseAuth.instance.currentUser!.uid);
Future<Profile> readUserDocument() {
return docRef.get().then(
(DocumentSnapshot doc) {
return Profile(
imagePaths: doc.get('imagePaths'),
firstName: doc.get('firstName'),
lastName: doc.get('lastName') ,
bio: doc.get('bio'),
gender: doc.get('gender'),
hometown: doc.get('hometown'),
age: doc.get('age'),
ethnicity: doc.get('ethnicity'),
hairColor: doc.get('hairColor'),
eyeColor: doc.get('eyeColor'),
hobbies: doc.get('hobbies'),
instagram: doc.get('instagram'),
snapchat: doc.get('snapchat'),
work: doc.get('work'),
school: doc.get('school'),
educationLevel: doc.get('educationLevel')
);
},
onError: (e) => print("Error getting document: $e"),
);
}
In a separate file, I define a list tile that calls readUserDocument()
in the onClick method
ListTile(
onTap: () async {
Profile userData = await readUserDocument(); // TYPE ERROR HERE
// ...
}
// ...
)
When calling await readUserDocument()
, the following exception is thrown:
_TypeError (type 'List<dynamic>' is not a subtype of type 'List<String>')
This confuses me because readUserDocument()
will return a Profile, not a List<dynamic>
. I am also confused as to why List<String>
is involved here.
Any help would be appreciated. Thanks!
2
Answers
I ended up taking a different approach and following the Firestore documentation example.
To my Profile object, I added the
factory Profile.fromFirestore()
andMap<String, dynamic> toFirestore()
methods. I was then able to use a converter to get the Profile objectMaybe you can try get the document snapshot and build the object when returned to the ListTile