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

后端 未结 8 1823
没有蜡笔的小新
没有蜡笔的小新 2021-02-12 13:26
Widget build(context) {
    try{
      if (isFirst == true) {
        fetchImage();
        fetchCategories(context);
        isFirst = false;
      }
    }catch(Excepti         


        
8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-12 13:41

    There are quick-and-dirty answer, and proper answer

    Quick-and-dirty

    Use list?.elementAt() ?? "" for safe access to element of a list

    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: [
                new Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    new InkResponse(
                        child: new Column(
                          children: [
                            Padding(
                              padding: EdgeInsets.all(10.0),
                              child: new Image.asset(
                                catimages?.elementAt(0) ?? "",
                                width: 60.0,
                                height: 60.0,
                              ),
                            ),
                            new Text(
                              categoriesText?.elementAt(0) ?? "",
                              style: TextStyle(color: Colors.white),
                            ),
                          ],
                        ),
                        onTap: () {
                          debugPrint("on tv clikced");
                          widget.fetchApI.fetchSubCategories(context, 6);
                        }),
                    new InkResponse(
                      child: new Column(
                        children: [
                          Padding(
                            padding: EdgeInsets.all(10.0),
                            child: new Image.asset(
                              catimages?.elementAt(1) ?? "",
                              width: 60.0,
                              height: 60.0,
                            ),
                          ),
                          new Text(
                            categoriesText?.elementAt(1) ?? "",
                            style: TextStyle(color: Colors.white),
                          ),
                        ],
                      ),
                      onTap: () {
                        debugPrint("on moview clicked");
                        widget. fetchApI.fetchSubCategories(context, 7);
                      },
                    ),
                    new InkResponse(
                      child: new Column(
                        children: [
                          Padding(
                            padding: EdgeInsets.all(10.0),
                            child: new Image.asset(
                              catimages?.elementAt(2) ?? "",
                              width: 60.0,
                              height: 60.0,
                            ),
                          ),
                          new Text(
                           categoriesText?.elementAt(2) ?? "",
                            style: TextStyle(color: Colors.white),
                          ),
                        ],
                      ),
                      onTap: () {
                        debugPrint("on news clicked");
                        widget.fetchApI.fetchSubCategories(context, 10);
                      },
                    ),
                    new InkResponse(
                      child: new Column(
                        children: [
                          Padding(
                            padding: EdgeInsets.all(10.0),
                            child: new Image.asset(catimages?.elementAt(3) ?? "",
                                width: 60.0, height: 60.0),
                          ),
                          new Text(
                            categoriesText?.elementAt(3) ?? "",
                            style: TextStyle(color: Colors.white),
                          ),
                        ],
                      ),
                      onTap: () {
                        debugPrint('on shows clicked');
                        widget.fetchApI.fetchSubCategories(context, 8);
                      },
                    ),
                    new InkResponse(
                      child: new Column(
                        children: [
                          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),
              ],
            ),
          ),
        );
      }
    }
    

    Proper answer

    Frankly, if I were to review this code, even if it works seamlessly, I would reject this change, because of the structure/pattern this code is using is quite bad.

    Please use FutureBuilder, StreamBuilder or ValueListenableBuilder instead, but you need to provide more code (especially fetchImage and fetchCategories) for us to help.

提交回复
热议问题