I was making a horizontal ListView.builder
of "Cards", but in the itemBuilder
option, in the item argument, I typed buildCard(item: int [index])
but it showed me the error :
The operator '[]' isn't defined for the type 'Type'. Try defining the operator '[]'.
I would like to solve this error please.
My code :
import 'package:flutter/material.dart';
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class CardItem {
final String assetImage;
final String title;
const CardItem({
required this.assetImage,
required this.title,
});
}
class _HomePageState extends State<HomePage> {
List<CardItem> items = [
CardItem(title: 'Card1', assetImage: 'images/movie/01.webp'),
CardItem(title: 'Card2', assetImage: 'images/movie/02.png'),
CardItem(title: 'Card3', assetImage: 'images/movie/03.png'),
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
margin: const EdgeInsets.symmetric(vertical: 20.0),
height: 250,
child: ListView.separated(
itemBuilder: (context, index) => buildCard(item: int[index]),
separatorBuilder: (context, _) => SizedBox(width: 12),
itemCount: 3)));
}
Widget buildCard({
required CardItem item,
}) =>
Container(
width: 170,
color: Colors.red,
child: Column(
children: [
Expanded(
child: Image(
image: AssetImage(item.assetImage),
fit: BoxFit.cover,
),
)
],
));
}
you need to use items list not int in buildCard(item: items[index])
class _HomePageState extends State<HomePage> {
List<CardItem> items = [
CardItem(title: 'Card1', assetImage: 'images/movie/01.webp'),
CardItem(title: 'Card2', assetImage: 'images/movie/02.png'),
CardItem(title: 'Card3', assetImage: 'images/movie/03.png'),
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
margin: const EdgeInsets.symmetric(vertical: 20.0),
child: ListView.separated(
itemBuilder: (context, index) => buildCard(item: items[index]),
separatorBuilder: (context, _) => SizedBox(height: 12),
itemCount: 3)));
}
Widget buildCard({
required CardItem item,
}) =>
Container(
width: 170,
height: 250,
color: Colors.red,
child: Column(
children: [
Expanded(
child: Image.network('https://github.com/flutter/plugins/raw/master/packages/video_player/video_player/doc/demo_ipod.gif?raw=true')
,
)
],
));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.