我想在 Theme 对象中解析我的 json :
结果:
[["[\"1\",\"Alternant\"]","[\"2\",\"DevOps\"]"]
方法 getListTheme :
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),
],
);
关于android - 在对象 flutter 中解析json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58102104/