[solved] Flutter Error: RangeError (index): Invalid value: Not in range 0..2, inclusive: 3

Hello Guys How are you all? Hope You all are fine. I am using Listview in my code All the items are rendering fine but also the following error: Flutter Error: RangeError (index): Invalid value: Not in range 0..2, inclusive: 3. So today Here I come with all possible solutions for this error.

We are providing you all possible solutions to solve this error. let’s start this article without wasting your time.

How Flutter Error: RangeError (index): Invalid value: Not in range 0..2, inclusive: 3 Occurs ?

I am using Listview in my code All the items are rendering fine but also the following error: 

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

Here Is my code

  List<String> itemList() {
    return ["Item 1", "Item 2", "Item 3"];
  }

  Widget generateList() {
    List<String> list = itemList();
    ListView myList = new ListView.builder(
      itemBuilder: (context, index) {
        return new ListTile(
          title: new Text(list[index]),
        );
      },
    );
    return myList;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('FlutterCorner'),
      ),
      body: Container(
        child: generateList(),
      ),
    );
  }

Exact Like Above Screenshot

How to solve Flutter Error: RangeError (index): Invalid value: Not in range 0..2, inclusive: 3 ?

Question : How to solve Flutter Error: RangeError (index): Invalid value: Not in range 0..2, incusive: 3 ?
Answer : The solution is simple you just have to add itemCount to the builder so that the builder allows it to know the item count. Just like the below Code.

Solution 1 : Add itemCount to the builder

The solution is simple you just have to add itemCount to the builder so that the builder allows it to know the item count. Just like the below Code.

  Widget generateList() {
    List<String> list = itemList();
    ListView myList = new ListView.builder(
      itemCount: list.length,
      itemBuilder: (context, index) {
        return new ListTile(
          title: new Text(list[index]),
        );
      },
    );
    return myList;
  }

This Solution worked for me. just add itemCount and give it list of length. So that builder allow to know how much item builder has to build.

Summery

So it’s all About All possible solutions. Hope this above all solution helped you a lot. Comment below Your thoughts and your queries. Comment Below on your suggestion.

Check Out Below Article

Milan Dhameliya is an Information Technology Engineer. By profession, he is a Full Stack developer with knowledge of multiple back-end platforms (e.g., PHP, Node.js, Python) and frontend Flutter frameworks.

Leave a Comment