android - 在对象 flutter 中解析json

标签 android mobile flutter flutter-layout

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

相关文章:

ruby-on-rails - 移动页面的单独移动 View 和布局

php - 如何处理移动设备发送的图像?

flutter - 与参数类型 'Object' 相关的错误无法分配给参数类型 'ImageProvider<Object>?'

http - 单击按钮以查看更改时,必须重新加载应用程序(Flutter)

android - 带有 PendingIntent 的 RequestLocationUpdates 永远不会触发

android - OnItemSelectedListener 错误是否有解决方法?

android - 我在模拟器中遇到问题

java - 无法使用 Appium 在 Android 中单击设备的位置权限对话框允许/拒绝按钮

Javax 包不存在

asynchronous - 如何解决将 Future<dynamic> 实例作为参数传递的错误?