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), ], ), ), ); }
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(); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With