'Migrating to null safety: The argument type 'Object?' can't be assigned to the parameter type 'DocumentSnapshot<Object?>'
After migrating to null safety, I'm getting the error: The argument type 'Object?' can't be assigned to the parameter type 'DocumentSnapshot<Object?>'. Thanks for helping out.
FutureBuilder(
future: usersReference
.doc(sublist[i].ownerId)
.get(),
builder: (context, snap) {
if (!snap.hasData) {
return loader();
}
UserModel user =
UserModel.fromDocument(snap.data);//error line
//tried snap.data(), snap.data!
return Container();
);
This the UserModel
class UserModel {
String id;
String username;
String url;
UserModel(
{
required this.id,
required this.username,
required this.url,
});
factory UserModel.fromDocument(DocumentSnapshot doc) {
return UserModel(
id: doc.id,
username: doc.get('username') ?? "",
url: doc.get('url') ?? "",
);
}
}
Solution 1:[1]
Try to adding type cast if u have to make sure it type.
Solution 2:[2]
Did you try to cast it?
FutureBuilder<DocumentSnapshot>(
future: usersReference
.doc(sublist[i].ownerId)
.get(),
builder: (BuildContext context, DocumentSnapshot snapshot) {
if (!snapshot.hasData) {
return loader();
}
UserModel user = UserModel.fromDocument(snap.data);
return Container();
}
);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tuan |
| Solution 2 |
