Saturday, October 3, 2020

Understand Map on DART/Flutter

Example 1 : Using a given json extract items and loop through subitems

var questions=[

{

'question':'What is your nationality?',

'answers':['Egyptians','USA','KSA']

},

{

'question':'What is your nationality?',

'answers':['Egyptians','USA','KSA']

},

];


To access first question: questions[0]['question']

To access first question answers list questions[0]['answers']

To Loop through first question answers list and extract them to a given list

MyList =[
    ...(questions[i]['answers'] as List<String>).map((answer){return(answer);}).toList()
];






Example 2 in Dart






Example 3 in Flutter

Step1: Define Class Object
class Transaction {
final String id;
final String title;
final double amount;
final DateTime date;
Transaction({@required this.id, @required this.title, @required this.amount, @required this.date});
}

Step2: Fill Cass Object List with data
final List<Transaction> transactions = [
Transaction( id: 't1', title: 'New Shoes', amount: 69.99, date: DateTime.now()),
Transaction( id: 't2', title: 'New Maps', amount: 69.99, date: DateTime.now()),
];

Step3: Loop Through List using MAP
body: Column(
children:
transactions.map((tx) { return Text( tx.title ); }).toList(),
)

The Complete working code












No comments: