我想把图像在资产文件夹,但这个错误反复发生:参数类型'对象?'不能分配给参数类型'字符串'。
这是我的产品。
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 = [~...
字符串
这是主代码:
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);
},
),
);
}
型
起初我以为这个错误是因为Product.dart中的“final List images;”,但无法通过将其定义更改为动态来解决问题。任何人都可以帮助将非常感谢
1条答案
按热度按时间oalqel3c1#
images属性是一个列表,所以你需要获取元素,而不是直接将它传递给一个参数,该参数应该是一个字符串:
字符串