import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
var items = [
"https://raw.githubusercontent.com/AmmarMohib/Economic-app/master/lib/1.jpeg",
"https://raw.githubusercontent.com/AmmarMohib/Economic-app/master/lib/2.jpeg",
"https://raw.githubusercontent.com/AmmarMohib/Economic-app/master/lib/3.jpeg",
"https://raw.githubusercontent.com/AmmarMohib/Economic-app/master/lib/4.jpeg"
];
var text =[
"Iphone 12",
"Iphone 12",
"Iphone 12",
"Iphone 12",
];
var reviews =[
"5.0 (23 reviews)"
"5.0 (23 reviews)"
"5.0 (23 reviews)"
"5.0 (23 reviews)"
];
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: "Economic app",
home: Home(),
);
}
}
class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(title: Text("Economic app",style: TextStyle(color: Colors.black,fontFamily: 'Lobster',fontWeight: FontWeight.w600,fontSize: 30),),
actions: <Widget>[
IconButton(
icon: Icon(
Icons.notifications_active,
color: Colors.black,
),
onPressed: () {
print(Text("hello"));
},
)
],centerTitle: true,backgroundColor: Colors.white,
),
body:GridView.count(
childAspectRatio: (45/15),
crossAxisCount: 1,
mainAxisSpacing: 7.5,
children: List.generate(items.length , (index) {
return Padding(
padding: const EdgeInsets.only(left:20.0,right: 20.0,top: 20.0),
child:ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Container(
color: Colors.white,
child: Center(child: Column(
children: [
GridTile(
child: Title(
color: Colors.red,
child: Row(
children: [
ClipRRect(
borderRadius: BorderRadius.circular(10),
child:
Image.network(items[index],height: 110,)
),
Padding(
padding: const EdgeInsets.only(bottom:75.0,left: 10),
child: Text(text[index],style: TextStyle(fontSize: 20,fontWeight: FontWeight.bold),),
),
// IconButton(
// icon: Icon(
// Icons.star_border_purple500,
// color: Colors.yellow,
// ),
// onPressed: () {
// },
// ),
Text(reviews[index])
],
)
)),
])
),
),
),
);
})
)
),
);
}
}
这是我的代码,我想实现文本抛出网格视图计数,我已经在顶部完成了但是当我在底部实现时它抛出了范围错误索引超出范围索引应该小于1:1,
我尝试了很多,但它抛出了这个错误,
请帮助我认为你会理解我的问题
答案 0 :(得分:1)
问题很可能是由这个 Text(reviews[index])
引起的。
对于您的 gridview,您引用的是基于 items.length
的长度。如果 items
的长度不等于 reviews
的长度,你会得到这样的错误。
尝试注释掉这个文本小部件。
这导致了您的问题:
var reviews = [
"5.0 (23 reviews)"
"5.0 (23 reviews)"
"5.0 (23 reviews)"
"5.0 (23 reviews)"
];
此列表的长度为 1
。通过添加逗号来修复它:
var reviews = [
"5.0 (23 reviews)",
"5.0 (23 reviews)",
"5.0 (23 reviews)",
"5.0 (23 reviews)"
];