Flutter:读取具有空数据的json时的问题

oyjwcjzk  于 2023-06-25  发布在  Flutter
关注(0)|答案(1)|浏览(124)

我试图从JSON响应中读取errorDetails
我的JSON响应:

用户名无效时

{
    "userData": null,
    "verificationStatus": false,
    "errorDetails": [
        {
            "errorMap": {
                "Username": "The Username Is Invalid"
            }
        }
    ],
    "siteUserHBList": null
}

密码无效时

{
    "userData": null,
    "verificationStatus": false,
    "errorDetails": [
        {
            "errorMap": {
                "Password": "The Password Is Invalid"
            }
        }
    ],
    "siteUserHBList": null
}

我的型号:

import 'dart:convert';

class LoginResponse {
  final UserData userData;
  final bool verificationStatus;
  final List<ErrorDetails> errorDetails;

  LoginResponse({required this.userData, required this.verificationStatus, required this.errorDetails});

  factory LoginResponse.fromJson(Map<String, dynamic> json) {
    return LoginResponse(
        verificationStatus: json['verificationStatus'] as bool,
        userData: UserData.fromJson(json['userData']) ,
        errorDetails: List<ErrorDetails>.from(json["errorDetails"].map((x) => ErrorDetails.fromJson(x))),
    );
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['userData'] = this.userData;
    data['verificationStatus'] = this.verificationStatus;
    data['errorDetails'] = List<dynamic>.from(errorDetails.map((x) => x.toJson()));
    return data;
  }
}

class ErrorDetails
{
  final ErrorMap errorMap;

  ErrorDetails({required this.errorMap });

  factory ErrorDetails.fromJson(Map<String, dynamic> json) {
    return ErrorDetails(
        errorMap: ErrorMap.fromJson(json['errorMap'])
    );
  }

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

class ErrorMap
{
  final String? Username; // nullable
  final String? Password; // nullable

  ErrorMap({this.Username, this.Password});

  factory ErrorMap.fromJson(Map<String, dynamic> json)
  {
    return ErrorMap
      (
      Username: json['Username'] as String?,
      Password: json['Password'] as String?,
    );
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['Username'] = Username;
    data['Password'] = Password;
    return data;
  }
}

class UserData{
  final int userId;
  final int applicationId;
  final int siteId;
  final String username;
  final String email;
  final String firstName;
  final String lastName;
  final String profileImageUrl;

  UserData({required this.userId, required this.applicationId, required this.siteId, required this.username,
  required this.email, required this.firstName, required this.lastName, required this.profileImageUrl});

  factory UserData.fromJson(Map<String, dynamic> json) {
    return UserData(
      userId: json['userId'] as int,
      applicationId: json['applicationId'] as int,
      siteId: json['siteId'] as int,
      username: json['username'] as String,
      email: json['email'] as String,
      firstName: json['firstName'] as String,
      lastName: json['lastName'] as String,
      profileImageUrl: json['profileImageUrl'] as String,
    );
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['userId'] = this.userId;
    data['applicationId'] = this.applicationId;
    data['siteId'] = this.siteId;
    data['username'] = this.username;
    data['email'] = this.email;
    data['firstName'] = this.firstName;
    data['lastName'] = this.lastName;
    data['profileImageUrl'] = this.profileImageUrl;
    return data;
  }
}

Main.dart:

Map<String, dynamic> parsedData = jsonDecode(response.body);
LoginResponse loginResponse = LoginResponse.fromJson(parsedData);
print('errormap:>>$loginResponse.errorDetails[0].errorMap.Username');

但我得到了下面的例外:
类型“_Map<String,dynamic>”不是类型“ErrorDetails”的子类型
当密码或用户名无效时,我的userData为null,否则返回用户详细信息。那么对于userData,我需要添加任何空检查吗?我该怎么做?

bybem2ql

bybem2ql1#

是的,你应该让userData为空,否则它会给予一个错误。为此,您应该在LoginResponse类中更改其类型,如下所示:

final UserData? userData;

在fromJson方法中,将其更改为:

factory LoginResponse.fromJson(Map<String, dynamic> json) {
  return LoginResponse(
    userData: json['userData'] == null ? null : UserData.fromJson(json['userData']), 
...

报告的错误异常是因为您在errorDetails字段中传递了一个map,也许您正在尝试不同的东西?如果我试着用你发布的两张Map,它可以工作。
同时避免使用“new”,必要时只使用“this”。
希望对你有帮助:)

相关问题