I have an error on the "snap.snapshot.value" parameter in the following line "var map = Map <String, dynamic> .from (snap.snapshot.value);". The error is "The argument type 'Object?' can't be assigned to the parameter type 'Map <dynamic, dynamic>'. "
class _HomePageState extends State<HomePage> {
List<Posts> postsList = [];
@override
void initState() {
super.initState();
DatabaseReference postsRef = FirebaseDatabase.instance.reference().child("Posts");
postsRef.once().then((snap) {
var map = Map<String, dynamic>.from(snap.snapshot.value); <--- the error is here
postsList.clear();
map.forEach((key, value) {
var values = Map<String,dynamic>.from(map);
Posts posts = Posts
(
values['url'],
values['descrizione'],
values['data'],
values['ora']
);
postsList.add(posts);
});
Would you change a code like below?
From
var map = Map<String, dynamic>.from(snap.snapshot.value);
To
Map<String, dynamic> map = snap.snapshot.value as Map<String, dynamic>
This is because Map.from function accepts a Map<dynamic, dynamic> but you are passing the object
instead. So to pass as the same use casting like below :
var map = Map<String, dynamic>.from(snap.snapshot.value as Map<dynamic, dynamic>);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With