Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Null safety errors on model from quicktype

I’m trying to use a json model for my project. This is theresult I get from quicktype:

import 'dart:convert';

Hisselist hisselistFromJson(String str) => Hisselist.fromJson(json.decode(str));

String hisselistToJson(Hisselist data) => json.encode(data.toJson());

class Hisselist {
  Hisselist({
    required this.code,
    required this.data,
  });

  String code;
  List<Datum> data;

  factory Hisselist.fromJson(Map<String, dynamic> json) => Hisselist(
        code: json["code"],
        data: List<Datum>.from(json["data"].map((x) => Datum.fromJson(x))),
      );

  Map<String, dynamic> toJson() => {
        "code": code,
        "data": List<dynamic>.from(data.map((x) => x.toJson())),
      };
}

class Datum {
  Datum({
    required this.id,
    required this.kod,
    required this.ad,
    required this.tip,
  });

  int id;
  String kod;
  String ad;
  Tip tip;

  factory Datum.fromJson(Map<String, dynamic> json) => Datum(
        id: json["id"],
        kod: json["kod"],
        ad: json["ad"],
        tip: tipValues.map[json["tip"]],
      );

  Map<String, dynamic> toJson() => {
        "id": id,
        "kod": kod,
        "ad": ad,
        "tip": tipValues.reverse[tip],
      };
}

enum Tip { HISSE }

final tipValues = EnumValues({"Hisse": Tip.HISSE});

class EnumValues<T> {
  Map<String, T> map;
  Map<T, String> reverseMap;

  EnumValues(this.map);

  Map<T, String> get reverse {
    if (reverseMap == null) {
      reverseMap = map.map((k, v) => new MapEntry(v, k));
    }
    return reverseMap;
  }
}

I added "required at Datum class for null safety but I can not these 2 problems :

error: Non-nullable instance field ‘reverseMap’ must be initialized.
(not_initialized_non_nullable_instance_field at [myapp]
lib/models/apis/hisselist.dart:69)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

error: The argument type ‘Tip?’ can’t be assigned to the parameter
type ‘Tip’. (argument_type_not_assignable at [myapp]
lib/models/apis/hisselist.dart:48)

How can I fix this? thanks for your help

>Solution :

It is possible to get null from reading json, thats why it is showing error on tipValues.map[json["tip"]] using bang! is risky, you can provide default value on null case like

tip: tipValues.map[json["tip"]]?? Tip.HISSE,

Or make tip nullable

  Tip? tip;

Now on EnumValues all item expected to be initialized, you can use late to promise that you will assign data before reading it
late Map<T, String> reverseMap;

or better make it nullable

 Map<T, String>? reverseMap;
class EnumValues<T> {
  Map<String, T> map;
  Map<T, String>? reverseMap;

  EnumValues(this.map);

  Map<T, String> get reverse {
    return reverseMap ??= map.map((k, v) => MapEntry(v, k));
  }
}

More about null-safety

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading