如何解决方法'[]'在null抖动上被调用

时间:2020-03-13 10:15:17

标签: flutter dart

在尝试解析json对象时,我一直收到此错误。对于用户,默认情况下,我的json对象中的某些字段为null待设置。但是一旦启动应用程序,我就需要检索它们。但是,由于这些空值,我无法将数据解析到我的对象中。

: [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The method '[]' was called on null.
E/flutter ( 9117): Receiver: null
E/flutter ( 9117): Tried calling: []("degree")

如何正确解决此问题。这是我的模型课。

User.dart

class User {

      final String first_name;
      final String email;
      final String last_name;
      final String country;
      final String gender;
      final String phone;
      final String degree;
      final String linkedin;
      final String institution;
      final String profile_image;
      final String created_at;
      final String updated_at;
      final String category;
      final String industry;
      final String bio_interest;
      final String fav_quote;
      final String isAdmin;
      final String current_job;
      final String company;
      final String position;
      final String state_of_origin;
      int id = 0;

      User(
          this.first_name,
          this.email,
          this.last_name,
          this.country,
          this.gender,
          this.phone,
          this.degree,
          this.institution,
          this.profile_image,
          this.created_at,
          this.updated_at,
          this.company,
          this.isAdmin,
          this.linkedin,
          this.category,
          this.industry,
          this.bio_interest,
          this.fav_quote,
          this.position,
          this.current_job,
          this.state_of_origin,
          this.id);

      Map<String, dynamic> toJson() => {
        'first_name': first_name,
        'email': email,
        'last_name': last_name,
        'country': country,
        'gender': gender,
        'phone': phone,
        'isAdmin': isAdmin,
        'company': company,
        'linkedin': linkedin,
        'position': position,
        'degree': degree,
        'institution': institution,
        'profile_image': profile_image,
        'created_at': created_at,
        'updated_at': updated_at,
        'category': category,
        'industry': industry,
        'bio_interest': bio_interest,
        'fav_quote': fav_quote,
        'current_job': current_job,
        'state_of_origin': state_of_origin,
        'id': id,
      };

      User.fromJson(Map<String, dynamic> json)
          : first_name = json['first_name'],
            email = json['email'],
            last_name = json['last_name'],
            country = json['country'],
            gender = json['gender'],
            phone = json['phone'],
            degree = json['education']['degree'],
            linkedin = json['employment']['linkedin'],
            institution = json['education']['institution'],
            position = json['employment']['position'],
            isAdmin = json['isAdmin'],
            profile_image = json['profile_image'],
            created_at = json['created_at'],
            updated_at = json['updated_at'],
            category = json['category'],
            industry = json['industry'],
            company = json['employment']['company'],
            bio_interest = json['bio_interest'],
            fav_quote = json['fav_quote'],
            current_job = json['current_job'],
            state_of_origin = json['state_of_origin'],
            id = json['id'];
    }

Json数据

{
    "id": 221,
    "category": "mentor",
    "email": "benmentor@gmail.com",
    "email_verified_at": null,
    "first_name": "Ben",
    "last_name": "mentor",
    "username": null,
    "country": "United States",
    "industry": null,
    "gender": "Male",
    "bio_interest": null,
    "phone": null,
    "state_of_origin": null,
    "fav_quote": null,
    "profile_image": "noimage.jpg",
    "terms": null,
    "isAdmin": null,
    "check_status": null,
    "current_job": null,
    "created_at": "2020-03-13 02:24:59",
    "updated_at": "2020-03-13 02:24:59",
    "social_id": null,
    "employment": null,
    "preference": [],
    "education": null
}

我意识到json数据中的某些字段为空。例如偏好和就业。有没有办法给他们默认值,所以我可以绕过此错误。

1 个答案:

答案 0 :(得分:0)

在访问列表中的数据之前添加空检查

class User {
  final String first_name;
  final String email;
  final String last_name;
  final String country;
  final String gender;
  final String phone;
  final String degree;
  final String linkedin;
  final String institution;
  final String profile_image;
  final String created_at;
  final String updated_at;
  final String category;
  final String industry;
  final String bio_interest;
  final String fav_quote;
  final String isAdmin;
  final String current_job;
  final String company;
  final String position;
  final String state_of_origin;
  int id = 0;

  User(
      this.first_name,
      this.email,
      this.last_name,
      this.country,
      this.gender,
      this.phone,
      this.degree,
      this.institution,
      this.profile_image,
      this.created_at,
      this.updated_at,
      this.company,
      this.isAdmin,
      this.linkedin,
      this.category,
      this.industry,
      this.bio_interest,
      this.fav_quote,
      this.position,
      this.current_job,
      this.state_of_origin,
      this.id);

  Map<String, dynamic> toJson() => {
        'first_name': first_name,
        'email': email,
        'last_name': last_name,
        'country': country,
        'gender': gender,
        'phone': phone,
        'isAdmin': isAdmin,
        'company': company,
        'linkedin': linkedin,
        'position': position,
        'degree': degree,
        'institution': institution,
        'profile_image': profile_image,
        'created_at': created_at,
        'updated_at': updated_at,
        'category': category,
        'industry': industry,
        'bio_interest': bio_interest,
        'fav_quote': fav_quote,
        'current_job': current_job,
        'state_of_origin': state_of_origin,
        'id': id,
      };

  User.fromJson(Map<String, dynamic> json)
      : first_name = json['first_name'],
        email = json['email'],
        last_name = json['last_name'],
        country = json['country'],
        gender = json['gender'],
        phone = json['phone'],
        degree = getFromList(json['education'], 'degree'),
        linkedin = getFromList(json['employment'], 'linkedin'),
        institution = getFromList(json['education'], 'institution'),
        position = getFromList(json['employment'], 'position'),
        isAdmin = json['isAdmin'],
        profile_image = json['profile_image'],
        created_at = json['created_at'],
        updated_at = json['updated_at'],
        category = json['category'],
        industry = json['industry'],
        company = getFromList(json['employment'], 'company'),
        bio_interest = json['bio_interest'],
        fav_quote = json['fav_quote'],
        current_job = json['current_job'],
        state_of_origin = json['state_of_origin'],
        id = json['id'];
}

String getFromList(Map<String, dynamic> json, String key) {
  return json != null ? json[key] : "";
}