如何创建具有颤振子模型的模型



我试图创建一个模型与子模型内部,但当尝试做数据或把数据在这个子模型犯错误:

模型:

class BannerTextFormat {
FontWeight ?bold;
double ?size;
Color ?color;
BannerTextFormat({this.bold, this.size, this.color});
Map<String, dynamic> toJson() => {
'bold': bold,
'size': size,
'color': color,
};
BannerTextFormat.fromJson(Map<String, dynamic> json) :
bold = json['bold'],
size = json['size'],
color = json['color']
;
}
class ContaBanner {
String ?texto;
String ?imageBackground;
Color ?colorBackground;
BannerTextFormat ?textFormat;
ContaBanner({this.texto, this.imageBackground, this.colorBackground, this.textFormat});
Map<String, dynamic> toJson() => {
'texto': texto,
'imageBackground': imageBackground,
'colorBackground': colorBackground,
'textformat': BannerTextFormat.fromJson(textFormat as Map<String, dynamic>)
};
}

和消息错误:null检查操作符对null值,在上面这行,这个错误显示当我执行应用程序,而不是当我编译,对不起,我的英语

ContaBanner contaBanner = ContaBanner();
contaBanner.textFormat?.size = 30; \error here

Color ?BannerTextFormat ?定义;

Java代码int颜色;字符串定义;

创建类模型的正确方法如下:

class BannerTextFormat {
FontWeight ?bold;
double ?size;
Color ?color;
BannerTextFormat({this.bold, this.size, this.color});
Map<String, dynamic> toJson() => {
'bold': bold,
'size': size,
'color': color,
};
BannerTextFormat.fromJson(Map<String, dynamic> json) :
bold = json['bold'],
size = json['size'],
color = json['color']
;
}
class ContaBanner {
String ?texto;
String ?imageBackground;
Color ?colorBackground;
BannerTextFormat ?textFormat;
ContaBanner({this.texto, this.imageBackground, this.colorBackground, this.textFormat});
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = Map<String, dynamic>();
data['texto'] = texto;
data['imageBackground'] = imageBackground;
data['colorBackground'] = colorBackground;
if (textFormat != null) {
data['textFormat'] = textFormat!.toJson();
}
return data;
}
ContaBanner.fromJson(Map<String, dynamic> json) :
texto = json['texto'],
imageBackground = json['imageBackground'],
colorBackground = json['colorBackground'],
textFormat = BannerTextFormat.fromJson(json['textFormat']);
}

工作完美

最新更新