RangeError (index): Invalid value: Not in range 0..3, inclusive: 4

来源:6-

qq_神兵小将_0

2019-03-16

报错RangeError (index): Invalid value: Not in range 0..3, inclusive: 4,一共五条内容,显示了四条,然后后边报错
http://img.mukewang.com/szimg/5c8cbda40001201803420172.jpg

写回答

4回答

金陵

2020-04-22

ListView.builder里边要添加itemCount属性

//img.mukewang.com/szimg/5e9fd3e609e79dca05970377.jpg

0
0

qq_神兵小将_0

提问者

2019-03-17

import 'package:flutter/material.dart';


class NewsPage extends StatefulWidget{
 @override
 _NewsPageState createState() =>_NewsPageState();
}


class _NewsPageState extends State{

 static List imgUrls =[
   'http://i2.chinanews.com/simg/hd/2018/09/30/smimg_effd92c23cfa49069d23499b45be2d64.jpg',
   'http://i2.chinanews.com/simg/hpic/2019/03-10/6d24b3b4dc2a438ca3d21c6e5b724036_sm.jpg',
   'http://i2.chinanews.com/simg/hd/2019/01/17/smimg_80feacef0abd42478e3fda3f536f040e.jpg',
   'http://www.chinanews.com/mil/2019/03-13/U776P4T8D8779333F19930DT20190313180128.jpg',
   'http://www.chinanews.com/mil/2019/03-13/U776P4T8D8779494F19930DT20190313215215.jpg'
 ];

 static List nTitles =[
   '军队代表委员:强军事业展现新气象新作为',
   '解放军和武警部队代表团举行第二次全体会议',
   '我们,为和平而来——军队代表委员谈中国海军走向深蓝',
   '“金龙-2019”中柬联合训练在柬埔寨开幕',
   '燃爆!《中国军队一分钟》震撼来袭'
 ];

 static Listlist =new List();

 //新闻右侧缩略图
 static Widget _rightImg(int index){
   return Container(
     width: 150,
     height: 100,
     decoration: BoxDecoration(
       shape: BoxShape.rectangle,
       borderRadius: BorderRadius.circular(10.0),
       image: DecorationImage(
         image: NetworkImage(imgUrls[index]),
       ),
     ),
   );
 }

 //右侧
 static Widget _leftColumn(int index) {
   //样式
   var titleStyle = TextStyle(color: Colors.black, fontSize: 16.0);
   var tagStyle =
   TextStyle(color: Color.fromARGB(255, 100, 100, 100), fontSize: 10.0);
   var cateStyle=TextStyle(fontSize: 14,color: Color.fromARGB(255, 100 , 100, 100));
   var timeStyle=TextStyle(fontSize: 14,color: Colors.black);

   //新闻标题
   var title=Text(
     nTitles[index],
     style: titleStyle,
     maxLines: 2,
     overflow: TextOverflow.ellipsis,
   );


   //新闻tag
   var tag=Row(
     children:list[index].tag.map((tag){
       return Padding(
         child:Container(
           child: Text(tag,style: tagStyle),
           padding: EdgeInsets.fromLTRB(4,2,4,2),
           decoration: BoxDecoration(
               color: Color.fromARGB(255, 200, 200, 200),
               shape: BoxShape.rectangle,
               borderRadius: BorderRadius.all(Radius.circular(10.0))
           ),
         ),
         padding: EdgeInsets.only(left: 3),
       );
     }).toList(),
   );

   //新闻栏目
   var cate=Text(list[index].nCate,style: cateStyle);

   //发布时间
   var time=Text(list[index].cTime,style: timeStyle);

   //组合
   return Column(
     children:[
       title,
       Container(
           margin: EdgeInsets.only(top: 8,bottom: 8),
           child: tag
       ),
       Row(
         children:[
           cate,time
         ],
       ),
     ],
   );
 }

 var listView =ListView.builder(
   itemBuilder: (BuildContext context,int index){
     return Container(
       margin: EdgeInsets.only(top: 10,left: 10,right: 10),
       padding: EdgeInsets.only(bottom: 10),
       decoration:BoxDecoration(
           border:Border(bottom: BorderSide(color: Color(0xfff3f3f3),width: 0.5))
       ),
       child: Row(
         children:[
           Expanded(child: _leftColumn(index)),
           _rightImg(index)
         ],
       ),
     );
   },
 );




 @override
 Widget build(BuildContext context) {

   for(int i=0 ; i<nTitles.length-1; i++){
     list.add(News(
         imgUrls[i],
         nTitles[i],
         '热点新闻',
         ['头条','今日'].toList(),
         '1552722410'
     ));

   }

   return Scaffold(
     body:Center(child:listView),
   );
 }

}

class News {
 String imgUrl;
 String nTitle;
 String nCate;
 Listtag;
 String cTime;

 News(this.imgUrl,this.nTitle,this.nCate,this.tag,this.cTime);
}

0
0

qq_神兵小将_0

提问者

2019-03-17

老师这是我的代码,时间转格式的也麻烦教我一下怎么弄
0
2
qq_神兵小将_0
回复
CrazyCodeBoy
完整的代码已经粘出来了,麻烦老师看一下,谢谢
2019-03-17
共2条回复

CrazyCodeBoy

2019-03-17

将相关完整代码贴出来,我帮你看一下具体是哪里的问题

0
1
qq_神兵小将_0
老师能帮看一下吗,代码已贴在下边
2019-03-19
共1条回复

Flutter从入门到进阶 实战携程网App 一网打尽核心技术

解锁Flutter开发新姿势,,系统掌握Flutter开发核心技术。

4788 学习 · 3277 问题

查看课程