How to add multiple items at once to a “List<Map<String, dynamic>>”

2,798 views
Skip to first unread message

Aly F.

unread,
Nov 4, 2020, 2:08:49 AM11/4/20
to 'Stuart Morgan' via Flutter Development (flutter-dev)
Good Evening/Morning Everyone,

I’m developing an application where I’m trying to add multiple items in a form to a temporary list for user preview on screen before saving the form and adding to SQlite. 

Attached is a screenshot of the screen, I want to be able to press the “Add Item to List” button and add the user input to the list and display for their preview before they can go ahead and finally press “Add List”
Does anyone have a work around for this?  



Thank You to all in advance.

Regards,

Aly.

Screen Shot 2020-11-04 at 02.04.30.png

Suzuki Tomohiro

unread,
Nov 4, 2020, 8:45:54 AM11/4/20
to 'Stuart Morgan' via Flutter Development (flutter-dev)
List has addAll method.

If this is not what you are looking for, would you explain the difficulty in detail?

--
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/2cc2110d-bde2-4ab3-856f-c09594c94933%40Spark.

Aly F.

unread,
Nov 4, 2020, 6:32:10 PM11/4/20
to Tomo Suzuki, 'Stuart Morgan' via Flutter Development (flutter-dev)
Hey Guys,

Thanks Suzuki for your response.
Actually that wasn’t it, but I figured out a way. 
So now I’m looking for examples on “List<Map<String, dynamic>>”
How to implement it.

Thanks all!

Regards,
Aly 

Suzuki Tomohiro

unread,
Nov 4, 2020, 6:43:45 PM11/4/20
to Flutter Development (flutter-dev)
Is this what you're looking for?

List<Map<String, dynamic>> list = [];

Aly F.

unread,
Nov 4, 2020, 6:49:49 PM11/4/20
to Flutter Development (flutter-dev), Suzuki Tomohiro
Yes it is. Thank You
But are there any examples to take a look at?
Like full code examples or code snippets.


Aly F.

unread,
Nov 4, 2020, 6:54:41 PM11/4/20
to Flutter Development (flutter-dev), Suzuki Tomohiro
Sorry my email cut off

Full code examples say for implementing class and class members or integrate it into another class…
Sorry for my questions, I’m still a bit new to Flutter & Dart.

Suzuki Tomohiro

unread,
Nov 4, 2020, 7:00:43 PM11/4/20
to Aly F., Flutter Development (flutter-dev)
Luke this? (Not sure about “integrating this to another class”)

List<Map<String, dynamic>> list = [];
Map<String, dynamic> map = <String, dynamic>{“foo”, 1};
list.add(map);

Aly F.

unread,
Nov 4, 2020, 7:17:10 PM11/4/20
to Suzuki Tomohiro, Flutter Development (flutter-dev)
I just made the 
List<Map<String, dynamic>> _tempItemList[];
Then made this addItemToList function.
Would this work?
Let me elaborate, when creating a model, how can we declare a list to be List<Map<String, dynamic>>?

Suzuki Tomohiro

unread,
Nov 4, 2020, 7:54:33 PM11/4/20
to Flutter Development (flutter-dev)
Nice. Your challenge is getting more clear. You can declare it with:
<Map<String, dynamic>>[];


Aly F.

unread,
Nov 4, 2020, 8:00:55 PM11/4/20
to Tomo Suzuki, 'Stuart Morgan' via Flutter Development (flutter-dev), Flutter Development (flutter-dev)
So Suzuki, 
I would have to write it like this? 

Suzuki Tomohiro

unread,
Nov 4, 2020, 9:46:12 PM11/4/20
to Flutter Development (flutter-dev)
Yes, that is correct.

But I recommend to define a class, such as GroceryItem, to clarify the meaning of the item.

Aly F.

unread,
Nov 4, 2020, 10:22:10 PM11/4/20
to Flutter Development (flutter-dev), Suzuki Tomohiro
So it would be like this correct?
And I would define class Item above in class Grocery for the "items”? 
Something like:
Final Items List<Map<String, dynamic>items?
I’m experimenting while you help me with this Suzuki
I appreciate your help

Suzuki Tomohiro

unread,
Nov 4, 2020, 11:38:44 PM11/4/20
to Flutter Development (flutter-dev)
Nice. I meant this:

final List<Item> items;

With Item class, you don't need to use Map<String, dynamic>, which is unclear what keys it should have. Item class is more clear for the keys and the types of the values. BTW, are you sure itemQuantity is a String?

Aly F.

unread,
Nov 5, 2020, 12:09:08 AM11/5/20
to Flutter Development (flutter-dev), Suzuki Tomohiro
Thank You Bro! Appreciate it
Now I’m getting the hang of it with you.

And it was a String as I was going to use a TextFormField but thinking of using a NumberPicker 
Reply all
Reply to author
Forward
0 new messages