Home > Software design >  The argument type 'Object?' can't be assigned to the parameter type 'Map<dyna
The argument type 'Object?' can't be assigned to the parameter type 'Map<dyna

Time:01-08

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);
      });

CodePudding user response:

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>

CodePudding user response:

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>);
  •  Tags:  
  • Related