我有一个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),
),
);
},
),
);
}
}
我在索引键item1
中输入了json对象的boolean
值.所以我得到了错误type 'bool' is not a subtype of type 'String'
.
因此,我想知道如何在ListView.builder
中显示json对象中除String
之外的所有类型的值(而不仅仅是boolean
),而不会出现错误.