- Published on
[Solved] The argument type ‘Null Function(DataSnapshot)’ can’t be assigned to the parameter type ‘FutureOr<dynamic> Function(DatabaseEvent)’

Problem: When trying to fetch data from a Firebase Realtime database in a Flutter application, the following error is thrown:
The argument type 'Null Function(DataSnapshot)' can't be assigned to the parameter type 'FutureOr<dynamic> Function(DatabaseEvent)'.
For the following code snippet:
DatabaseReference _dbRef = FirebaseDatabase.instance.ref();
_dbRef.once().then((DataSnapshot dataSnapshot) {
print("read once" + dataSnapshot.value.toString());
});
Solution: Since the once()
method returns a DatabaseEvent
and not DatabaseSnapshot
, the incorrect parameter type in the then()
callback is the cause of this error. As the DatabaseSnapshot
class is encapsulated in the DatabaseEvent
class, we have to use its snapshot
field to access the DatabaseSnapshot
instance. Here is the syntactically correct version of the code snippet above:
DatabaseReference _dbRef = FirebaseDatabase.instance.ref();
_dbRef.once().then((DatabaseEvent databaseEvent) {
print("read once" + databaseEvent.snapshot.value.toString());
});
Conclusion
Thanks for reading this blog post!
If you have any questions or concerns please feel free to post a comment in this post and I will get back to you when I find the time.
If you found this article helpful please share it and make sure to follow me on Twitter and GitHub, connect with me on LinkedIn and subscribe to my YouTube channel.