skip to Main Content
List<PopularBookModel> populars = popularBookData
    .map((item) => PopularBookModel(item['title'], item['author'],
        item['price'], item['image'], item['color'], item['description']))
    .toList();

Result:

The argument type 'Object?' can't be assigned to the parameter type.

What should I do?

2

Answers


  1. It look like item is of type Map<String, Object>, but the constructor of PopularBookModel takes specific types, most likely String. Try and see if this works:

    List<PopularBookModel> populars = popularBookData
        .map((item) => PopularBookModel(
            item['title'] as String,
            item['author'] as String,
            item['price'] as String,
            item['image'] as String,
            item['color'] as String,
            item['description'] as String))
        .toList();
    

    Or whatever type you expect to be there.

    Login or Signup to reply.
  2. Please try the below code

    class PopularBookModel {
      final String id;
      final String title;
      final String author;
      final String price;
      final String image;
      final String color;
      final String description;
    
      const PopularBookModel(
        this.id,
        this.title,
        this.author,
        this.price,
        this.image,
        this.color,
        this.description
      );
    }
    

    After creating model, Now create a variable to use

    late dynamic popularBookData;
    late List<PopularBookModel> populars;
    
    @override
    void initState(){
      // Create your own function to fetch data from your server and define it to popularBookData
      populars = popularBookData.map((item) {
        return PopularBookModel(
          item['id'].toString(),
          item['title'].toString(),
          item['author'].toString(),
          item['price'].toString(),
          item['image'].toString(),
          item['color'].toString(), 
          item['description'].toString(),
        );
      }).toList();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search