Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I resolve "The argument type 'Object?' can't be assigned to the parameter type 'String'"?

I wanted to put images which were in assets folder but this error happens repeatly:
The argument type ‘Object?’ can’t be assigned to the parameter type ‘String’.

this is my product.dart:

class Product {
  final String title, description;
  final List<String> images;
  final double rating, price;
  final bool isFavourite, isPopular;

  Product(
      {required this.images,
      this.rating = 0.0,
      this.isFavourite = false,
      this.isPopular = false,
      required this.title,
      required this.price,
      required this.description});
}

List<Product> demoProducts = [~...

and this is main code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import '../../../models/Product.dart';

class ItemListPage extends StatefulWidget {
  const ItemListPage({super.key});

  @override
  State<ItemListPage> createState() => _ItemListPageState();
}

class _ItemListPageState extends State<ItemListPage> {
  final NumberFormat numberFormat = NumberFormat('###,###,###,###');
  List<Product> productList = [
    Product(
        images: ["assets/images/abocado.png"],
        title: "1",
        price: 5000,
        description: 'description',
        rating: 3.5,
        isFavourite: false,
        isPopular: true),
    Product(
        images: ["assets/images/abocado.png"],
        title: "2",
        price: 5000,
        description: 'description',
        rating: 3.5,
        isFavourite: false,
        isPopular: true),
    Product(
        images: ["assets/images/abocado.png"],
        title: "3",
        price: 5000,
        description: 'description',
        rating: 3.5,
        isFavourite: false,
        isPopular: false),
    Product(
        images: ["assets/images/abocado.png"],
        title: "4",
        price: 5000,
        description: 'description',
        rating: 3.5,
        isFavourite: false,
        isPopular: false),
    Product(
        images: ["assets/images/abocado.png"],
        title: "5",
        price: 5000,
        description: 'description',
        rating: 3.5,
        isFavourite: false,
        isPopular: false),
    Product(
        images: ["assets/images/abocado.png"],
        title: "6",
        price: 5000,
        description: 'description',
        rating: 3.5,
        isFavourite: false,
        isPopular: false),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("product lists"),
        centerTitle: true,
      ),
      body: GridView.builder(
        itemCount: productList.length,
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
          childAspectRatio: 0.9,
          crossAxisCount: 2,
          // mainAxisExtent: 250,
        ),
        itemBuilder: (context, index) {
          return productContainer(
              productName: productList[index].title ?? "",
              productImageUrl: productList[index].images ?? "", // error happens here 
              price: productList[index].price ?? 0);
        },
      ),
    );
  }

At first i thought this error occurs because of ‘final List images;‘ in Product.dart but couldn’t solve the problem by changing it’s definition into dynamic.
Anyone can help will be much appriciate

>Solution :

The images property is a list, so you need to take the element instead of directly passing it to an argument that supposedly expects a string:

itemBuilder: (context, index) {
          return productContainer(
              productName: productList[index].title ?? "",
              productImageUrl: productList[index].images?.firstOrNull ?? "", // takes the first element 
              price: productList[index].price ?? 0);
        },
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading