请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

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

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

正在回答 回答被采纳积分+3

4回答

金陵 2020-04-22 13:19:52

ListView.builder里边要添加itemCount属性

https://img1.sycdn.imooc.com//szimg/5e9fd3e609e79dca05970377.jpg

0 回复 有任何疑惑可以回复我~
提问者 qq_神兵小将_0 2019-03-17 14:01:53

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 回复 有任何疑惑可以回复我~
提问者 qq_神兵小将_0 2019-03-17 07:30:58
老师这是我的代码,时间转格式的也麻烦教我一下怎么弄
0 回复 有任何疑惑可以回复我~
  • 代码贴的的不全,build方法只有一半,将这个类的完整代码已通过代码的形式贴出来,另外日期format,我将答案写在了:http://coding.imooc.com/learn/questiondetail/107894.html
    回复 有任何疑惑可以回复我~ 2019-03-17 13:02:06
  • 提问者 qq_神兵小将_0 回复 CrazyCodeBoy #2
    完整的代码已经粘出来了,麻烦老师看一下,谢谢
    回复 有任何疑惑可以回复我~ 2019-03-17 14:03:12
CrazyCodeBoy 2019-03-17 01:16:17

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

0 回复 有任何疑惑可以回复我~
  • 提问者 qq_神兵小将_0 #1
    老师能帮看一下吗,代码已贴在下边
    回复 有任何疑惑可以回复我~ 2019-03-19 17:03:17
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号