Flutter中debug模式未触发‘Null不是String子类型’错误,打包后报错原因?
Flutter序列化问题:Debug模式不报错但Release模式崩溃的解决与调试
问题背景
后端返回的User数据中,language字段为可选(有时不返回),当前代码将其声明为late String language,导致打包成IPA/APK真机运行时抛出type 'Null' is not a subtype of type 'String'错误,但VSCode Debug模式下无报错。需要实现两个目标:让Debug模式提前触发该错误,同时正确修复模型定义。
如何让Debug模式触发空赋值错误
- 添加断言检查:在
fromJson方法中加入断言,Debug模式下会直接抛出错误,Release模式断言自动失效:User.fromJson(Map<String, dynamic> json) { // ...其他字段赋值 language = json['language']; assert(language != null, 'language字段不能为null'); // ...其他字段赋值 } - 立即访问变量:赋值后强制访问
language,触发late变量的空访问错误:User.fromJson(Map<String, dynamic> json) { // ...其他字段赋值 language = json['language']; language.toString(); // 强制访问,Debug模式下null会报错 // ...其他字段赋值 } - 严格声明空类型:移除
late关键字,将language声明为String?,让空安全检查在Debug和Release模式下都生效:String? language; // 替换原late String language;
正确修复User模型
因为language是可选字段,推荐按以下方式修改模型:
方式1:允许language为null
class User { late int id; late String firstName; late String lastName; String? birthdate; late bool showBirthDate; late UserStatus status; late String description; late String phone; late String email; String? language; // 声明为可空类型 late String image; User({ required this.id, required this.firstName, required this.lastName, this.birthdate, required this.showBirthDate, required this.status, required this.description, required this.phone, required this.email, this.language, // 设为可选参数 required this.image, }); User.fromJson(Map<String, dynamic> json) { id = json['id']; firstName = json['firstName']; lastName = json['lastName']; birthdate = json['date_of_birth']; showBirthDate = json['show_birth_date'] == 10; status = UserStatus.fromStaus(json['status']); description = json['description']; phone = json['phone']; email = json['email']; language = json['language']; // 赋值null不会触发错误 image = json['image']; } }
方式2:给language设置默认值
如果业务上不允许language为null,可以设置默认值:
class User { late int id; late String firstName; late String lastName; String? birthdate; late bool showBirthDate; late UserStatus status; late String description; late String phone; late String email; late String language; // 保持非空 late String image; User({ required this.id, required this.firstName, required this.lastName, this.birthdate, required this.showBirthDate, required this.status, required this.description, required this.phone, required this.email, this.language = 'en', // 设置默认值 required this.image, }); User.fromJson(Map<String, dynamic> json) { id = json['id']; firstName = json['firstName']; lastName = json['lastName']; birthdate = json['date_of_birth']; showBirthDate = json['show_birth_date'] == 10; status = UserStatus.fromStaus(json['status']); description = json['description']; phone = json['phone']; email = json['email']; language = json['language'] ?? 'en'; // 无返回时用默认值 image = json['image']; } }
内容的提问来源于stack exchange,提问作者Bohdan Kikacheishvili
相关产品推荐
相关产品推荐

