Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RangeError (index): Invalid value: Valid value range is empty: 0

I am trying to fetch a list from API that is two methods fetchImages and fetchCategories. the first time it is showing a red screen error and then after 2 seconds automatically it is loading the list. Can you please tell me what's the issue with my code and how to avoid showing that red screen error in my app?

Widget build(context) {     try{       if (isFirst == true) {         fetchImage();         fetchCategories(context);         isFirst = false;       }     }catch(Exception){      }      return MaterialApp(       home: Scaffold(         backgroundColor: Colors.black,         appBar: AppBar(           title: Text('Lets see images!'),         ),         body: new Column(           children: <Widget>[             new Row(               mainAxisAlignment: MainAxisAlignment.center,               children: <Widget>[                 new InkResponse(                     child: new Column(                       children: <Widget>[                         Padding(                           padding: EdgeInsets.all(10.0),                           child: new Image.asset(                             catimages[0],                             width: 60.0,                             height: 60.0,                           ),                         ),                         new Text(                           categoriesText[0],                           style: TextStyle(color: Colors.white),                         ),                       ],                     ),                     onTap: () {                       debugPrint("on tv clikced");                       widget.fetchApI.fetchSubCategories(context, 6);                     }),                 new InkResponse(                   child: new Column(                     children: <Widget>[                       Padding(                         padding: EdgeInsets.all(10.0),                         child: new Image.asset(                           catimages[1],                           width: 60.0,                           height: 60.0,                         ),                       ),                       new Text(                         categoriesText[1],                         style: TextStyle(color: Colors.white),                       ),                     ],                   ),                   onTap: () {                     debugPrint("on moview clicked");                     widget. fetchApI.fetchSubCategories(context, 7);                   },                 ),                 new InkResponse(                   child: new Column(                     children: <Widget>[                       Padding(                         padding: EdgeInsets.all(10.0),                         child: new Image.asset(                           catimages[2],                           width: 60.0,                           height: 60.0,                         ),                       ),                       new Text(                        categoriesText[2],                         style: TextStyle(color: Colors.white),                       ),                     ],                   ),                   onTap: () {                     debugPrint("on news clicked");                     widget.fetchApI.fetchSubCategories(context, 10);                   },                 ),                 new InkResponse(                   child: new Column(                     children: <Widget>[                       Padding(                         padding: EdgeInsets.all(10.0),                         child: new Image.asset(catimages[3],                             width: 60.0, height: 60.0),                       ),                       new Text(                         categoriesText[3],                         style: TextStyle(color: Colors.white),                       ),                     ],                   ),                   onTap: () {                     debugPrint('on shows clicked');                     widget.fetchApI.fetchSubCategories(context, 8);                   },                 ),                 new InkResponse(                   child: new Column(                     children: <Widget>[                       Padding(                         padding: EdgeInsets.all(10.0),                         child: new Image.asset('assets/live_icon.png',                             width: 60.0, height: 60.0),                       ),                       new Text(                         'Live',                         style: TextStyle(color: Colors.white),                       ),                     ],                   ),                   onTap: () {                     debugPrint('on live clicked');                   },                 ),               ],             ),             ImageList(images,widget.fetchApI),           ],         ),       ),     );   } 
like image 240
Mounika Avatar asked Mar 04 '19 06:03

Mounika


1 Answers

Make sure specifying the length of the list of data. For example, if you're using ListView.builder give proper value to the attribute itemCount.

ListView.builder(             itemCount: snapshot.data.length,             itemBuilder: (ctx, index) {               return WidgetItem();             }); 
like image 173
Farwa Avatar answered Sep 19 '22 04:09

Farwa