我有一个 json 对象,我想使用 ListView.builder
可视化键和键值:
class Items {
Map<String, dynamic> getItems() {
String jsonData = '{ "item1": true,"item2": "value2","item3": "value3"}';
Map<String, dynamic> data = jsonDecode(jsonData);
return data;
}
}
class App extends StatefulWidget {
const App({Key? key}) : super(key: key);
@override
_AppState createState() => _AppState();
}
class _AppState extends State<App> {
@override
Widget build(BuildContext context) {
Map<String, dynamic> data = Items().getItems();
return Scaffold(
body: ListView.builder(
itemCount: data.keys.length,
itemBuilder: (c, index) {
return ListTile(
title: Text(
"Value " + data.values.toList()[index],
style: TextStyle(color: Colors.white),
),
subtitle: Text(
"Key " + data.keys.toList()[index],
style: TextStyle(color: Colors.white),
),
);
},
),
);
}
}
我在 key item1
中将 json 对象定义为一个 boolean
值。所以我收到错误类型'bool'不是类型'String'的子类型
。
所以我想知道如何从 ListView.builder
中的 json 对象显示除 String
之外的所有类型的值(不仅仅是 boolean
) code> 没有收到错误。
最佳答案
使用data.values.toList()[index].toString();
关于flutter - 如何在 json 对象中将字符串以外的其他类型的键值转换为 dart 中的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71533094/