Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The argument type 'Object?' can't be assigned to the parameter type 'Map<dynamic, dynamic>'

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);
      });
like image 489
Emilio Verde Avatar asked Oct 14 '25 19:10

Emilio Verde


2 Answers

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>
like image 198
KuKu Avatar answered Oct 17 '25 10:10

KuKu


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>);
like image 21
Diwyansh Avatar answered Oct 17 '25 10:10

Diwyansh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!