gpt4 book ai didi

android - 在对象 flutter 中解析json

转载 作者:行者123 更新时间:2023-11-30 05:01:51 25 4
gpt4 key购买 nike

我想在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com