You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter代码抛出Null变量异常:解决'String子类型不匹配'问题

问题解决方法

错误原因

问题出在User.fromJson方法中:直接从Map取值赋值给非空的String类型属性,若解析后的JSON里某个字段为null(比如旧数据缺失字段、数据损坏),就会触发Null转String的类型错误。另外要确保UserPreferences.init()在调用getUser()前执行,否则late修饰的_preferences会因未初始化引发其他异常。

修复步骤

1. 修复User.fromJson方法

给每个String类型字段添加null兜底逻辑,避免Null赋值给非空属性:

static User fromJson(Map<String, dynamic> json) => User(
  imagePath: json["imagePath"] ?? UserPreferences.myUser.imagePath,
  name: json["name"] ?? UserPreferences.myUser.name,
  email: json["email"] ?? UserPreferences.myUser.email,
  mobile: json["mobile"] ?? UserPreferences.myUser.mobile,
  address: json["address"] ?? UserPreferences.myUser.address,
  userid: json["userid"] ?? UserPreferences.myUser.userid,
  isDarkMode: json["isDarkMode"] ?? false,
);

如果不需要默认用户数据,也可以用空字符串兜底:

static User fromJson(Map<String, dynamic> json) => User(
  imagePath: json["imagePath"] ?? "",
  name: json["name"] ?? "",
  email: json["email"] ?? "",
  mobile: json["mobile"] ?? "",
  address: json["address"] ?? "",
  userid: json["userid"] ?? "",
  isDarkMode: json["isDarkMode"] ?? false,
);

2. 确保UserPreferences初始化顺序

在App启动时先完成SharedPreferences初始化,修改main函数:

void main() async {
  // 必须添加这句,确保Flutter绑定初始化完成
  WidgetsFlutterBinding.ensureInitialized();
  // 先初始化UserPreferences
  await UserPreferences.init();
  runApp(MyApp());
}

3. 可选:用json_serializable简化序列化(推荐)

如果不想手动编写序列化代码,可以用json_serializable包自动生成,减少出错概率:

  • 在pubspec.yaml添加依赖:
dependencies:
  json_annotation: ^4.8.1

dev_dependencies:
  build_runner: ^2.4.4
  json_serializable: ^6.7.0
  • 运行命令生成序列化代码:
flutter pub run build_runner build
  • 修改User类适配:
import 'package:json_annotation/json_annotation.dart';

part 'user.g.dart';

@JsonSerializable()
class User{
  final String imagePath;
  final String name;
  final String email;
  final String mobile;
  final String address;
  final String userid;
  final bool isDarkMode;

  const User ({
    required this.imagePath,
    required this.name,
    required this.email,
    required this.mobile,
    required this.address,
    required this.userid,
    required this.isDarkMode,
});

  User copy({
    String? imagePath,
    String? name,
    String? email,
    String? mobile,
    String? address,
    String? userid,
    bool? isDarkMode,
}) =>
  User (
    imagePath: imagePath ?? this.imagePath,
    name:  name ?? this.name,
    email: email ?? this.email,
    mobile:  mobile ?? this.mobile,
    address: address ?? this.address,
    userid: userid ?? this.userid,
    isDarkMode: isDarkMode ?? this.isDarkMode,
  );

  factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);

  Map<String, dynamic> toJson() => _$UserToJson(this);
}

额外说明

  • 基础修复不需要额外依赖,json_serializable属于可选优化工具
  • 若旧数据已损坏,可清除App存储后重新测试,避免解析异常

内容的提问来源于stack exchange,提问作者Devendiran

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 08:55:22