如何修复错误:“对空值使用空检查运算符”?

时间:2021-06-06 08:35:15

标签: json flutter class dart dart-null-safety

我在颤振中遇到 Null check operator used on a null value 错误。

Map<String, dynamic> newUserMap = jsonDecode(authenticate.body);
print('newUserMap?');
var authNodeUser = TokenContent.fromJson(newUserMap);
print('authNodeUser?');
String? jwtToken = authNodeUser.jwtToken;
print('jwtToken = ' + authNodeUser.jwtToken!);

当我看到 newUserMap?authNodeUser? 时,我猜测错误发生在这里;

String? jwtToken = authNodeUser.jwtToken;
print('jwtToken = ' + authNodeUser.jwtToken!);

这是 TokenContent 类;

class TokenContent {
  int? currUser;
  String? jwtToken;
  String? refreshToken;

  TokenContent({this.currUser, this.jwtToken, this.refreshToken});

  TokenContent.fromJson(Map<String, dynamic> json) {
    currUser = json['currUser'];
    jwtToken = json['token'];
    refreshToken = json['refreshToken'];
  }

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

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

打印 newUserMap 并确保 jwtToken 存在于 http 响应中。