-1

I am using the count variable as a record of the index of my List with type TagColumn. In my code, there is a plus button. Every time you press it, it is supposed to check, if you answered the previous text field. If you did, a new TagColumn will be created. In other words, a new text field. However, I am getting a RangeError, while I am using SetState() to update my count.

My code:

class Practice extends StatefulWidget {
      @override
      _PracticeState createState() => _PracticeState();
    }

class _PracticeState extends State<Practice>{
  int count = 0;
  @override
  Widget build(BuildContext context){
    List<TagColumn> ok = List.generate(count, (int i) => new TagColumn());
    return Scaffold(
      backgroundColor: Colors.black,
      body: new LayoutBuilder(builder: (context, constraint){
      return new Stack(
        children: <Widget>[
          SingleChildScrollView(
            child: SafeArea(
              child: new Wrap(
                direction: Axis.horizontal,
                children: ok,
              )
            ),
          ),
          new Positioned(
            child: new Align(
              alignment: FractionalOffset.bottomRight,
              child: Container(
                margin: EdgeInsets.only(bottom: 50.0, right: 40.0),
                child: RawMaterialButton(
                  onPressed: (){
                    setState(() {
                      if(count != 0 && ok[count].text.text.isEmpty){

                      }
                      else{
                        count ++;
                      }
                    });
                  },
                  shape: CircleBorder(),
                  child: Icon(
                    Icons.add_circle,
                    size: 100.0,
                    color: Color(0xffd3d3d3),
                  ),
                )
              )
            )
          )

        ],
      );
      }),
    );
  }
}

The error:

RangeError (index): Invalid value: Only valid value is 0: 1

If you need more context, here is a previous question I had about an early error in my code: Getter _text isn't defined for class TagColumn in Flutter

Ashu Mundra
  • 45
  • 1
  • 5

1 Answers1

1

here, when you are checking for last tag is empty or not then it throw error because index start from 0, so last element will be count-1.

Moreover, you are creating list every time, so text will remove from text field. to avoid you can create list outside build method and add item every time.

following code help you more to understand.

 int count = 0;
  List<TagColumn> ok = List();   // list created out side 
 @override
  Widget build(BuildContext context) {
    // List<TagColumn> ok = List.generate(count, (int i) => new TagColumn());  // commented list
    return Scaffold(
      backgroundColor: Colors.black,
      body: new LayoutBuilder(builder: (context, constraint) {
        return new Stack(
          children: <Widget>[
            SingleChildScrollView(
              child: SafeArea(
                  child: new Wrap(
                direction: Axis.horizontal,
                children: ok,
              )),
            ),
            new Positioned(
                child: new Align(
                    alignment: FractionalOffset.bottomRight,
                    child: Container(
                        margin: EdgeInsets.only(bottom: 50.0, right: 40.0),
                        child: RawMaterialButton(
                          onPressed: () {
                            setState(() {
                              if (count != 0 &&
                                  ok[count - 1].text.text.isEmpty) { // cout check change
                              } else {
                                ok.add(TagColumn());  // add new item on click
                                count++;
                              }
                            });
                          },
                          shape: CircleBorder(),
                          child: Icon(
                            Icons.add_circle,
                            size: 100.0,
                            color: Color(0xffd3d3d3),
                          ),
                        ))))
          ],
        );
      }),
    );
  }
Viren V Varasadiya
  • 25,492
  • 9
  • 45
  • 61