如何解决在空抖动上调用了方法[[]]

本软

尝试解析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'];
    }

杰森数据

{
    "id": 221,
    "category": "mentor",
    "email": "[email protected]",
    "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数据中的某些字段为null。例如偏好和就业。有没有办法给他们默认值,所以我可以绕过此错误。

Nidheesh

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

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] : "";
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在空对象上调用方法?

如何解决此问题:尝试在空对象引用上调用虚拟方法'android.text.Editable android.widget.EditText.getText()'

如何解决它在laravel 5.3中的字符串上调用成员函数diffForHumans()

NoSuchMethodError:在空值上调用了吸气剂“长度”

在null上调用了方法“ add”

如何解决-尝试在空对象引用上调用虚拟方法'void android.app.ProgressDialog.dismiss()'

在null上调用了方法'didAddToChannel'

如何解决此“在布尔值上调用成员函数row()”错误?

如何解决位置插补引起的抖动

如何解决在空对象引用上调用虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)'的尝试

Flutter:Geolocator返回在空值上调用了“ compareTo”方法

如何解决相机抖动中照片的旋转错误?

如何处理此抖动异常(未处理的异常:NoSuchMethodError:方法“ findAncestorStateOfType”在null上调用)

如何解决未处理的异常:NoSuchMethodError:在null上调用setter'dateTime ='

如何解决某些图像上的抖动“ Infinity或NaN toInt”

如何解决函数调用?

如何解决流程中的“无法在对象类型上调用构造函数”错误?

如何解决尝试在空对象引用上调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)”

在 null 上调用成员函数 get(),如何解决?

如何解决此错误“方法'/'被调用为空”

如何解决在flutter中在null上调用getter'value'的问题

NoSuchMethodError: 在 null 上调用了方法“findAncestorStateOfType”

在 null 上调用了方法“findRenderObject”

Flutter 错误:在 null 上调用了方法“validate”。接收者:空尝试调用:validate()

NoSuchMethodError:在 null 上调用了方法“isBefore”

NoSuchMethodError: 在 null 上调用了方法“[]”。(扑)

如何解决必须在 Xamarin 的主线程上调用权限请求?

在 null 上调用了方法“>=”。接收方:空

Flutter - 在 Null 上调用了方法“*”