【问题标题】:Assign value to a variable from list object converted from JSON in Flutter在 Flutter 中从 JSON 转换的列表对象中为变量赋值
【发布时间】:2021-03-18 10:12:50
【问题描述】:

我真的是 Flutter 的新手。我尝试搜索这个问题,虽然我找到了解决方案,但并不是我的所有问题都得到解决,因为大多数问题只是使用返回。

我有一个从 API 调用这里得到的 JSON:

{
    "error": false,
    "message": "LOGIN_SUCCESS",
    "user": {
        "id": 1219,
        "email": "john@example.com"
        "name": "John Doe",
        "category": 1,
        "branch": 1004,
        "lastlogin": "2020-12-04 03:12:43"
    }
}

我已经为用户创建了如下类

class User {
  int id;
  String name;
  String email;
  String category;
  String branch;
  String lastLogin;

  User({
    this.id,
    this.name,
    this.email,
    this.category,
    this.branch,
    this.lastLogin
  });

  factory User.fromJson(Map<String, dynamic> datauser){
    return User(
        id: datauser['id'],
        name: datauser['name'],
        email: datauser['email'],
        category: datauser['category'],
        branch: datauser['branch'],
        lastLogin: datauser['lastlogin']
    );
  }

}

还有一个如下的结果类..

class Result {

  String message;
  User user;

  Result({
    this.message,
    this.user
  });

  factory Result.fromJson(Map<String, dynamic> resultData){
    return Result(
        message: resultData['message'],
        user: User.fromJson(resultData['user'])
    );
  }

}

现在我的问题来了,因为我不知道如何从这一点继续前进

login() async {
    List<User> users;
    final response = await http.post("myUrlWithAPIcalls",
        body: {"email": email, "password": password});
    final data = jsonDecode(response.body);
    var rest = data['user'] as List;
    users = rest.map<User>((json) => User.fromJson(json)).toList();
}

所以问题是,如何将我从已转换为列表的 JSON 中获得的值分配给变量?

现在在示例中,如果它只是一个简单的 JSON 对象,我可以这样做..

final data = jsonDecode(response.body);
    int id = data['id'];
    String name = data['name'];
    String email = data['email'];
    String category = data['category'];
    String branch = data['branch'];

但我怎样才能在列表对象中做到这一点?

任何人都想知道为什么我会这样做,我试图将数据保存到我从其他人的代码中复制的 sharedpref 类中。

【问题讨论】:

    标签: json list flutter


    【解决方案1】:

    共享首选项并不意味着存储对象。使用类似sqflite 的东西来持久化对象(官方食谱here)。

    我不明白为什么你的 JSON 会显示一个用户的数据,但 login() 函数似乎解码了一个用户列表。

    我猜这就是你想要的:

    login() async {
        final response = await http.post("myUrlWithAPIcalls",
            body: {"email": email, "password": password});
        final data = jsonDecode(response.body);
        var user = User.fromJson(data['user']); // the variable you want
    }
    

    你没有说login() 函数在哪里,或者你想用那个User 对象做什么。仅供参考,Flutter 的重要组成部分是state management

    【讨论】:

    • 我正在使用共享首选项,因为我只想暂时保留数据。在原生 android 中,我曾经为此使用共享首选项,因为 sqlite 会将数据保存在设备中。我将暂时研究 sqflite。我的 JSON 仅解码 1 个用户数据,以返回目前正在尝试登录和使用该应用程序的用户。至于您的答案,我如何从用户那里逐一操作值?例如。电子邮件、姓名等。
    • 嗯,值在用户对象中,例如user.nameuser.branch 等。共享首选项与 sqflite 一样持久,它们只会在您卸载时消失。如果您只想在应用打开时保留数据,请使用我所说的状态管理解决方案。
    • 是的。这样可行!我将学习更多关于状态管理的知识。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2012-02-17
    • 2018-08-14
    • 1970-01-01
    • 2015-04-14
    • 1970-01-01
    • 2012-02-16
    • 1970-01-01
    • 2015-08-11
    相关资源
    最近更新 更多