Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select an item from a List in flutter

I have list from a model like this

amount:"12000"
dateTime:"19/07/2018"
detail:"Soto"
hashCode:853818549
id:1
name:"Theodorus"

I want to just select amount and add it to another list of string, but I'm always getting this error A value of type 'String' can't be assigned to a variable of type 'List<String>'. , I thinks its because im not doing it right, here is my code below

void setupList() async {
    DebtDatabase db = DebtDatabase();
    listCache = await db.getMyDebt();
    setState(() {
      filtered = listCache;
    });
     List<String> amount = new List<String>();
    listCache.map((value)  {
      amount = value.amount;   } );
    //print(amount);
  } 

can anyone help me, so I can get list of ammount from this model list and then sum all the ammount?

like image 813
Theodorus Agum Gumilang Avatar asked Jul 20 '18 04:07

Theodorus Agum Gumilang


People also ask

How do you scroll a list in flutter?

All you have to do is set Global Keys for your widgets and call Scrollable. ensureVisible on the key of your widget you want to scroll to. For this to work your ListView should be a finite List of objects. If you are using ListView.

How does list view work in flutter?

In Flutter, ListView is a scrollable list of widgets arranged linearly. It displays its children one after another in the scroll direction i.e, vertical or horizontal. There are different types of ListViews : ListView.


1 Answers

The map function returns an iterable and you can then transform it into a list. You should try something like this:

void setupList() async {
  DebtDatabase db = DebtDatabase();
  listCache = await db.getMyDebt();
  setState(() {
    filtered = listCache;
  });
  List<String> amount = listCache.map((value) => value.amount).toList();
  //print(amount);
}
like image 161
Romain Rastel Avatar answered Sep 30 '22 23:09

Romain Rastel