我想用 future bool 保存列表项可能吗?

I want to save list item with future bool is it possible?

Future<bool> login({username, password}) async {
    var api = API();
    _status = LoginStatus.loading;
    notifyListeners();
    var url = Uri.parse(api.baseUrl + api.auth);
    final response = await http.post(
      url,
      body: jsonEncode({
        "identifier": "$username",
        "password": "$password",
      }),
      headers: <String, String>{
        'Content-Type': 'application/json; charset=UTF-8',
      },
    );
    if (response.statusCode == 200) {
      final parsed = jsonDecode(response.body).cast<Map<String, dynamic>>();
      parsed
          .map<UserModel>((json) => UserModel.fromJson(json))
          .toList();
      final token = jsonDecode(response.body)['jwt'];
      print(token);
      await saveToken(token);
      return true;
    } else {
      _status = LoginStatus.error;
      _error = response.body;
      notifyListeners();
      return false;
    }
  }

Code Screen Shot

我应该如何将这个已解析的 JSON 保存到 UserModel?我遇到了很多问题,自己想出了很多东西,但我还不能给模型添加数据。

顺便说一下,我使用 strapi 作为后端,每个 api 都在工作。而且我还使用一个名为 json 的网站来飞镖转换器,以便我的模型是正确的(正如我所假设的那样)。

请帮忙!!!!!!!!!!!!!

用户模型

class UserModel {
  User user;

  UserModel({this.user});

  UserModel.fromJson(Map<String, dynamic> json) {
    user = json['user'] != null ? new User.fromJson(json['user']) : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.user != null) {
      data['user'] = this.user.toJson();
    }
    return data;
  }
}

class User {
  int id;
  String username;
  String email;
  String provider;
  bool confirmed;
  bool blocked;
  Role role;
  String displayName;
  String createdAt;
  String updatedAt;
  Avatar avatar;
  List<UserCategories> userCategories;

  User(
      {this.id,
      this.username,
      this.email,
      this.provider,
      this.confirmed,
      this.blocked,
      this.role,
      this.displayName,
      this.createdAt,
      this.updatedAt,
      this.avatar,
      this.userCategories});

  User.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    username = json['username'];
    email = json['email'];
    provider = json['provider'];
    confirmed = json['confirmed'];
    blocked = json['blocked'];
    role = json['role'] != null ? new Role.fromJson(json['role']) : null;
    displayName = json['displayName'];
    createdAt = json['created_at'];
    updatedAt = json['updated_at'];
    avatar =
        json['avatar'] != null ? new Avatar.fromJson(json['avatar']) : null;
    if (json['user_categories'] != null) {
      userCategories = new List<UserCategories>();
      json['user_categories'].forEach((v) {
        userCategories.add(new UserCategories.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['username'] = this.username;
    data['email'] = this.email;
    data['provider'] = this.provider;
    data['confirmed'] = this.confirmed;
    data['blocked'] = this.blocked;
    if (this.role != null) {
      data['role'] = this.role.toJson();
    }
    data['displayName'] = this.displayName;
    data['created_at'] = this.createdAt;
    data['updated_at'] = this.updatedAt;
    if (this.avatar != null) {
      data['avatar'] = this.avatar.toJson();
    }
    if (this.userCategories != null) {
      data['user_categories'] =
          this.userCategories.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Role {
  int id;
  String name;
  String description;
  String type;

  Role({this.id, this.name, this.description, this.type});

  Role.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
    description = json['description'];
    type = json['type'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['name'] = this.name;
    data['description'] = this.description;
    data['type'] = this.type;
    return data;
  }
}

class Avatar {
  int id;
  String name;
  String alternativeText;
  String caption;
  int width;
  int height;
  Formats formats;
  String hash;
  String ext;
  String mime;
  double size;
  String url;
  Null previewUrl;
  String provider;
  Null providerMetadata;
  String createdAt;
  String updatedAt;

  Avatar(
      {this.id,
      this.name,
      this.alternativeText,
      this.caption,
      this.width,
      this.height,
      this.formats,
      this.hash,
      this.ext,
      this.mime,
      this.size,
      this.url,
      this.previewUrl,
      this.provider,
      this.providerMetadata,
      this.createdAt,
      this.updatedAt});

  Avatar.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
    alternativeText = json['alternativeText'];
    caption = json['caption'];
    width = json['width'];
    height = json['height'];
    formats =
        json['formats'] != null ? new Formats.fromJson(json['formats']) : null;
    hash = json['hash'];
    ext = json['ext'];
    mime = json['mime'];
    size = json['size'];
    url = json['url'];
    previewUrl = json['previewUrl'];
    provider = json['provider'];
    providerMetadata = json['provider_metadata'];
    createdAt = json['created_at'];
    updatedAt = json['updated_at'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['name'] = this.name;
    data['alternativeText'] = this.alternativeText;
    data['caption'] = this.caption;
    data['width'] = this.width;
    data['height'] = this.height;
    if (this.formats != null) {
      data['formats'] = this.formats.toJson();
    }
    data['hash'] = this.hash;
    data['ext'] = this.ext;
    data['mime'] = this.mime;
    data['size'] = this.size;
    data['url'] = this.url;
    data['previewUrl'] = this.previewUrl;
    data['provider'] = this.provider;
    data['provider_metadata'] = this.providerMetadata;
    data['created_at'] = this.createdAt;
    data['updated_at'] = this.updatedAt;
    return data;
  }
}

class Formats {
  Thumbnail thumbnail;

  Formats({this.thumbnail});

  Formats.fromJson(Map<String, dynamic> json) {
    thumbnail = json['thumbnail'] != null
        ? new Thumbnail.fromJson(json['thumbnail'])
        : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.thumbnail != null) {
      data['thumbnail'] = this.thumbnail.toJson();
    }
    return data;
  }
}

class Thumbnail {
  String name;
  String hash;
  String ext;
  String mime;
  int width;
  int height;
  double size;
  Null path;
  String url;

  Thumbnail(
      {this.name,
      this.hash,
      this.ext,
      this.mime,
      this.width,
      this.height,
      this.size,
      this.path,
      this.url});

  Thumbnail.fromJson(Map<String, dynamic> json) {
    name = json['name'];
    hash = json['hash'];
    ext = json['ext'];
    mime = json['mime'];
    width = json['width'];
    height = json['height'];
    size = json['size'];
    path = json['path'];
    url = json['url'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['name'] = this.name;
    data['hash'] = this.hash;
    data['ext'] = this.ext;
    data['mime'] = this.mime;
    data['width'] = this.width;
    data['height'] = this.height;
    data['size'] = this.size;
    data['path'] = this.path;
    data['url'] = this.url;
    return data;
  }
}

class UserCategories {
  int id;
  String categoryName;
  int usersPermissionsUser;
  String publishedAt;
  String createdAt;
  String updatedAt;

  UserCategories(
      {this.id,
      this.categoryName,
      this.usersPermissionsUser,
      this.publishedAt,
      this.createdAt,
      this.updatedAt});

  UserCategories.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    categoryName = json['categoryName'];
    usersPermissionsUser = json['users_permissions_user'];
    publishedAt = json['published_at'];
    createdAt = json['created_at'];
    updatedAt = json['updated_at'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['categoryName'] = this.categoryName;
    data['users_permissions_user'] = this.usersPermissionsUser;
    data['published_at'] = this.publishedAt;
    data['created_at'] = this.createdAt;
    data['updated_at'] = this.updatedAt;
    return data;
  }
}

UserModel.fromJson 方法仅在 response.body 内容为 User 时有效,因此要解决您的问题,请不要在 response.body 上使用 UserModel.fromJson =] json 解码变量,而是对您确信符合您的 class 定义的数据使用 fromJson() 函数。

例如:

class User{
  final String name;
  final String surname;
  final String email;

  User({this.name, this.surname, this.email});

  factory User.fromJson(Map<String,dynamic> json){
    return User(
      'name': json['name'],
      'surname': json['surname'],
      'email': json['email'] 
    );
  }
}

从 api 收到的 json 响应:

{
  "name" : "John",
  "surname": "Smith",
  "email": "johnsmith@mail.com"
}

在您的函数中,解码响应并转换为用户模型 class:

final parsed = jsonDecode(response.body);  // or Map<String,dynamic> parsed = jsonDecode(response.body);
User user = User.fromJson(parsed)