0

I am currently developing an app in Dart/Flutter and I am facing one issue with my data class to handle my GraphQL response.

My User class is defined as following:

import 'package:json_annotation/json_annotation.dart';
import '/src/dataclasses/user_address.dart';
import '/src/dataclasses/user_birthdate.dart';
import '/src/dataclasses/user_legal_guardian.dart';

part 'user.g.dart';

@JsonSerializable()
class User {
  String? sub;
  String? email;
  String? username;
  List<dynamic>? roles;
  String? name;
  String? firstName;
  String? lastName;
  bool? newsletterSubscription;
  String? avatarUrl;
  String? backgroundUrl;
  bool? emailVerified;
  Birthdate? birthdate;
  Address? address;
  LegalGuardian? legalGuardian;
  String? externalSource;

  User(
      {this.email,
      this.username,
      this.roles,
      this.name,
      this.firstName,
      this.lastName,
      this.newsletterSubscription,
      this.avatarUrl,
      this.backgroundUrl,
      this.emailVerified,
      this.sub,
      this.birthdate,
      this.address,
      this.legalGuardian,
      this.externalSource});

  factory User.fromJson(Map<String, dynamic> data) {
    return User(
      sub: data['sub'],
      email: data['email'],
      username: data['username'],
      roles: data['roles'],
      name: data['name'],
      firstName: data['firstName'],
      lastName: data['lastName'],
      newsletterSubscription: data['newsletterSubscription'],
      avatarUrl: data['avatarUrl'],
      backgroundUrl: data['backgroundUrl'],
      emailVerified: data['emailVerified'],
      birthdate: Birthdate.fromJson(data['birthdate']),
      address: Address.fromJson(data['address']),
      legalGuardian: LegalGuardian.fromJson(data['legalGuardian']),
      externalSource: data['externalSource'],
    );
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['sub'] = sub;
    data['email'] = email;
    data['username'] = username;
    data['roles'] = roles;
    data['name'] = name;
    data['firstName'] = firstName;
    data['lastName'] = lastName;
    data['newsletterSubscription'] = newsletterSubscription;
    data['avatarUrl'] = avatarUrl;
    data['backgroundUrl'] = backgroundUrl;
    data['emailVerified'] = emailVerified;
    data['birthdate'] = birthdate;
    data['address'] = address;
    data['legalGuardian'] = legalGuardian;
    data['externalSource'] = externalSource;

    return data;
  }
}

I have the problem, that e.g. LegalGuardian can be in some cases null. In those cases I am receiving the error:

[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: type 'Null' is not a subtype of type 'Map<String, dynamic>'

My data class LegalGuardian looks like following:

import 'dart:convert';

import 'package:json_annotation/json_annotation.dart';
import '/src/dataclasses/user_birthdate.dart';

part 'user_legal_guardian.g.dart';

@JsonSerializable()
class LegalGuardian {
  String? email;
  String? firstName;
  String? lastName;
  String? phone;
  Birthdate? birthdate;
  bool? consent;

  LegalGuardian(
      {this.email,
      this.firstName,
      this.lastName,
      this.phone,
      this.birthdate,
      this.consent});

  LegalGuardian.empty();

  factory LegalGuardian.fromJson(Map<String, dynamic> data) {
    return LegalGuardian(
        email: data['email'],
        firstName: data['firstName'],
        lastName: data['lastName'],
        phone: data['phone'],
        birthdate: Birthdate.fromJson(json.decode(data['birthdate'])),
        consent: data['consent']);
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['email'] = email;
    data['firstName'] = firstName;
    data['lastName'] = lastName;
    data['phone'] = phone;
    data['birthdate'] = birthdate;
    data['consent'] = consent;
    return data;
  }
}

Does anyone has an idea how to solve that?

Best A

1 Answer 1

1

this : LegalGuardian.fromJson(data['legalGuardian']) => need a map here not a null.

method factory LegalGuardian.fromJson(<expected a map<String, dynamic>>) but since this data['legalGuardian] can be null, this function will throw datatype error.

before you call fromJson(Map<String, dynamic>) you must sure this map is not a null, so:

factory User.fromJson(Map<String, dynamic> data) {
    final mapLG = data['legalGuardian'];
    return User(
      sub: data['sub'],
      email: data['email'],
      username: data['username'],
      roles: data['roles'],
      name: data['name'],
      firstName: data['firstName'],
      lastName: data['lastName'],
      newsletterSubscription: data['newsletterSubscription'],
      avatarUrl: data['avatarUrl'],
      backgroundUrl: data['backgroundUrl'],
      emailVerified: data['emailVerified'],
      birthdate: Birthdate.fromJson(data['birthdate']),
      address: Address.fromJson(data['address']),
      // nullable
      legalGuardian: mapLG != null ? LegalGuardian.fromJson(data['legalGuardian']) : null,
      externalSource: data['externalSource'],
    );
  }

i see you using a build runner to generate model boiler plate, but i prefer to use dart data class generator for vscode its might come handy

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.