Flutter dataclass for GraphQL response – value can be another class or null

Flutter dataclass for GraphQL response – value can be another class or null


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

Share
Improve this question


Load 5 more related questions


Show fewer related questions

0

Reset to default



Leave a Reply

Your email address will not be published. Required fields are marked *