我想解析我在主题对象中的json:
结果:
["[\"1\",\"替代"]","[\"2\",\">DevOps\"]"]
方法获取列表主题:
Future<List<String>> getListTheme() async {
List<String> themes = List<String>();
final SharedPreferences preferences = await SharedPreferences.getInstance();
for (String key in preferences.getKeys()) {
if (key == 'token') {
preferences.containsKey(key);
} else {
themes.add(jsonEncode(preferences.getStringList(key)));
}
}
return themes;
}
其他代码 :
await Theme().getListTheme().then((value){
theme = jsonEncode(value);
});
我的主题课:
class Theme {
int id;
String name;
Theme({this.id, this.name});
factory Theme.fromJson(Map<String, dynamic> json) {
return Theme(
id: json['id'],
name: json['name'],
);
}
}
如何在主题对象中解析结果?
我想你可以尝试像这样返回一个传递 JSON 的对象
Future<Theme> fechDataTheme(){
Theme theme = Theme.fromJson('YOUR JSON HERE');
return theme;
}
一旦你有了对象,你就可以使用FutureBuilder在UI上获取它
。FutureBuilder<Theme>(
future: fetchDataTheme(),
builder: (context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
return Column(
children: <Widget>[
Text(snapshot.data.id),
Text(snapshot.data.name),
],
);
我使用这个网站将json转换为dart对象 https://javiercbk.github.io/json_to_dart/