我的模型如下:

class Abc extends Model {
  int playful;

  Abc({this.playful}) : super(id);

  Abc.fromJson(Map<String, dynamic> json) : this(playful: json['playful']);

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

我从playful获取整数值json(它是1或0),但是我想在我的应用程序中将其用作boolean(在应用程序中作为bool接收,但在数据库中作为int发送)。我该如何实现?

最佳答案

试试这个,

class Abc extends Model {
  bool playful;

  Abc({this.playful}) : super(id);

  Abc.fromJson(Map<String, dynamic> json) : this(playful: json['playful'] == 1);

  Map<String, dynamic> toJson() {
    final data = <String, dynamic>{};
    data['playful'] = this.playful ? 1 : 0;
    return data;
  }
}

10-08 00:50