0
votes

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'],
    );
  }
2
where are you getting this error? as in, which piece of code is throwing the error? This is just some missing type error and nullable issue. types can be fixed by letting the compiler know that it is a Map<String, dynamic>, through explicitly type casting it. - Tharun K

2 Answers

2
votes

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');
1
votes

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.