I have a following list and I want to add new items to that list when I click a button. How to achieve it?
List<dynamic> list = [
{
'id': 0,
'leading': 'Payment Application',
'trailing': 'System',
},
{
'id': 1,
'leading': 'Reference Number',
'trailing': 'SYM12113OI',
},
{
'id': 2,
'leading': 'Total',
'trailing': '\$15.00',
},
{
'id': 3,
'leading': 'Details',
'trailing': 'Civil Employment',
},
];
CodePudding user response:
Try the following code:
TextButton(
child: Text("child"),
onPressed: () {
list.add(value);
}
),
CodePudding user response:
1st create a Model for Your List Object : Example:
// To parse this JSON data, do
//
// final listModel = listModelFromJson(jsonString);
import 'dart:convert';
List<ListModel> listModelFromJson(String str) => List<ListModel>.from(json.decode(str).map((x) => ListModel.fromJson(x)));
String listModelToJson(List<ListModel> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class ListModel {
ListModel({
this.id,
this.leading,
this.trailing,
});
int id;
String leading;
String trailing;
factory ListModel.fromJson(Map<String, dynamic> json) => ListModel(
id: json["id"],
leading: json["leading"],
trailing: json["trailing"],
);
Map<String, dynamic> toJson() => {
"id": id,
"leading": leading,
"trailing": trailing,
};
}
Now You can define your List Like this:
List<ListModel> _list = [];
For Add Data In your List you can do :
_list.add(ListModel(id:1022,leading:"leading name",trailing:"training part "));