Hi Flutter Community,
I'm trying to add items to a Lis<dynamic> dynamically. This is how I declared my list.
List<dynamic> ingredientes = [];
I'm then assigning values from Firestore to this List, inside initState(),
ingredientes = widget.productId.data()['ingredientesArray'];
Finally, I'm using a ListView.builder to construct and display this List, returning the 'Text'
for each element here.
ListView.builder(
shrinkWrap:
true,
physics:
NeverScrollableScrollPhysics(),
itemCount: ingredientes.length,
itemBuilder:
(context,
index) {
return...
-----------------------------------------------------------------------------------------
Everything works fine so far... except that when I try to add items dynamically to this list, from another List, I get the following error: RangeError (index): Invalid value: Not in inclusive range 0..5: 7
This is how I'm trying this:
//A few things to note here:
//1: 'ingredientes' is the List I want to add to
//2: snapshot.data.docs is my 2nd List..
//3: 'snapshot.data.docs' is a List<String>, so in Theory I think this should match the <dynamic> type of ingredientes
//4: If I remove the line " ingredientes.add(snapshot.data.docs[index]);" and simply have the " ingredientes.length = ingredientes.length + 1" &. the line ""print (ingredientes.length);" , the length of ingredientes actually increases after I tap an item of my 'snapshot.data.docs' list. (Please refer to my screenshots attached here).
But when I have the following lines, I get the error mentioned above... (RangeError (index): Invalid value: Not in inclusive range 0..5: 7):
return GestureDetector(
onTap: () {
setState(() {
ingredientes.length = ingredientes.length + 1;
ingredientes.add(snapshot.data.docs[index]);
});
print(ingredientes
.length);
},
-------------------------------------------
It's as if the ListView.builder cannot rebuild itself, even though I'm increasing the number of "itemCount" by 1 every time I want to add a new String (and adding the String).
If this indeed is my error, how can I solve it so that my ListView.builder changes dynamically and I can add a String dynamically?
Thanks, and all best,
A.V.