[英]Flutter ListTile title name from json
I am trying to display both names of a person as a title in listtile flutter.我试图在 listtile flutter 中显示一个人的两个名字作为标题。 This is the sample json file
这是示例 json 文件
var users = const [
{
"first_name": "melissa",
"last_name": "fleming",
"phone_number": "0740-304-475"
},
{
"first_name": "christoffer",
"last_name": "christiansen",
"phone_number": "05761325"
},
{
"first_name": "valtteri",
"last_name": "pulkkinen",
"phone_number": "041-829-79-61"
}
]
This is the flutter code这是 flutter 代码
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text(
"List of Customers",
),
body: ListView.separated(
itemCount: users.length,
separatorBuilder: (context, index) => Divider(),
itemBuilder: (BuildContext context, int index) {
var user = users[index];
return ListTile(
title: Text(user['first_name']),
isThreeLine: true,
);
},
),
);
}
How can I pass both names to this part如何将两个名称都传递给这部分
title: Text(user['first_name']),
You can use for in loop and store the value in a String您可以在循环中使用 for 并将值存储在字符串中
for(var i in users){
String name = i['first_name'] + " "+ i['last_name'];
print(name);}
ListView.separated(
itemCount: users.length,
separatorBuilder: (context, index) => Divider(),
itemBuilder: (BuildContext context, int index) {
var user = users[index];
return ListTile(
title: Text(name),
isThreeLine: true,
);
},
),
there are two ways you can do it.有两种方法可以做到。
First Approach: if you want to show full name as title:第一种方法:如果你想显示全名作为标题:
title : Text("$users[0]['first_name'] $users[0]['last_name']")
Second Approach: if you want to show first name and last name separately use subtitle:第二种方法:如果要分别显示名字和姓氏,请使用字幕:
ListTile(
leading: const Icon(Icons.flight_land),
title: const Text($users[0]['first_name']),
subtitle: Text($users[0]['last_name']),
onTap: () { /* react to the tile being tapped */ }
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.