Pointer
Pointer

Reputation: 2186

Flutter fill DropdownMenuItem from list

My list contain id and name.

List myList = [];

for(var i = 0; i< myList.length; i++) {
        print("${myList[i].id}" + ' ' + "${myList[i].name}");
}

In console get:

I/flutter ( 7412): 10 ACCOUNTING

I/flutter ( 7412): 20 RESEARCH

I/flutter ( 7412): 30 SALES

I/flutter ( 7412): 40 OPERATIONS

When try populate DropdownMenuItem

items: myList.map((item) {
return new DropdownMenuItem<String>(
  value: item["id"].toString(),
  child: new Text(
    item["name"],
  ),
);
}).toList(),

Get error:

Class 'Dept' has no instance method '[]'.

Receiver: Instance of 'Dept'

Tried calling: [ ](" id ")

Upvotes: 1

Views: 198

Answers (1)

QAMAR
QAMAR

Reputation: 2694

Assuming myList is of type Dept

items: myList.map((item) {
    return new DropdownMenuItem<Dept>(
      value: item.id,
      child: new Text(
        item.name,
      ),
    );
    }).toList(),

Upvotes: 2

Related Questions