Add Item to a List Dynamically

465 views
Skip to first unread message

Andrew Villegas

unread,
Apr 23, 2021, 10:28:54 PM4/23/21
to Flutter Development (flutter-dev)
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.
Screen Shot 2021-04-23 at 21.17.54.png

Suzuki Tomohiro

unread,
Apr 23, 2021, 10:35:11 PM4/23/21
to Andrew Villegas, Flutter Development (flutter-dev)
Do you know which variable is having the list which is smaller than the index?

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/68301584-fef1-4273-86ca-91fdfbd6ef9dn%40googlegroups.com.

Andrew Villegas

unread,
Apr 23, 2021, 10:39:09 PM4/23/21
to Suzuki Tomohiro, Flutter Development (flutter-dev)
I think it's my ingredientes list, given that this is the list I'm using for my ListView.builder.

Suzuki Tomohiro

unread,
Apr 23, 2021, 10:54:18 PM4/23/21
to Flutter Development (flutter-dev)
I think it's "snapshot.data.docs" that is shorter than index. Would you use breakpoints and the debugger to see which list is throwing the error?

Screen Shot 2021-04-23 at 22.50.42.png

Andrew Villegas

unread,
Apr 23, 2021, 11:09:47 PM4/23/21
to Suzuki Tomohiro, Flutter Development (flutter-dev)
(to the whole group)
Thanks for replying, Suzuki.

Sure thing, I'll use the breakpoints and debugger right now, although I thought this line would ensure they were of equal length

ingredientes = widget.productId.data()['ingredientesArray'];

--A.V.

You received this message because you are subscribed to a topic in the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/flutter-dev/VHB3WoIK99k/unsubscribe.
To unsubscribe from this group and all its topics, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/CAHWORbq1V-s_sSouuYnBv%2B1hmt%2B%3Di0ezwxhPNgXa9vUauJAxgQ%40mail.gmail.com.

Andrew Villegas

unread,
Apr 24, 2021, 12:08:28 AM4/24/21
to Suzuki Tomohiro, Flutter Development (flutter-dev)
I figured out what my mistake was, Suzuki. It was rather a silly one (I was displaying my 'snapshot' list instead of my ingredientes list). Therefore, when I removed one item from ingredientes the indexes didn't quite match because I was reading the wrong list. 
Again, super silly.

Thank you so much for helping me figure this out though! It means a lot.

All best, and have a good night,
A.V. 

You received this message because you are subscribed to a topic in the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/flutter-dev/VHB3WoIK99k/unsubscribe.
To unsubscribe from this group and all its topics, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/CAHWORbq1V-s_sSouuYnBv%2B1hmt%2B%3Di0ezwxhPNgXa9vUauJAxgQ%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages