Reputation: 250
I'm getting the error on the code "snapshot.Data()". This is one of the breaking changes to Firestore, and snapshot is now of type "DocumentSnapshot<Object?>".
BREAKING: Getting a snapshots' data via the data getter is now done via the data() method.
The example supplied in the new documentation wasn't helpful for me.
"The argument type 'Object?' can't be assigned to the parameter type 'Map<String, dynamic>'"
I've tried a number of combinations, but I can't get rid of the error. Also, I'm not using a stream, I just want to read the single record.
Here is the code:
var snapshot = await _reference.doc(_user.uid).get();
return UserData.fromMap(snapshot.data());
Here is the model "fromMap":
factory UserData.fromMap(Map<String, dynamic> map) {
return UserData(
birthday: DateTime.fromMillisecondsSinceEpoch(map['birthday']),
gender: map['gender'],
isDarkMode: map['isDarkMode'],
isMetric: map['isMetric'],
name: map['name'],
password: map['password'],
);
}
Upvotes: 1
Views: 1547
Reputation: 250
Ok, found my issue. I had "_reference" defined like this:
final CollectionReference _reference = FirebaseFirestore.instance.collection('users');
I should have done it this way:
final CollectionReference<Map<String, dynamic>> _reference = FirebaseFirestore.instance.collection('users');
Or just this:
final _reference = FirebaseFirestore.instance.collection('users');
Upvotes: 2
Reputation: 12353
Change it to this:
factory UserData.fromMap(Map<String, dynamic>? map) //added a "?" after dynamic.
This makes your fromMap method accept nullable values, which in theory, your firebase query can return a null if the document doesn't exist.
Upvotes: 1