Home > Mobile >  Null safety error on model file from quicktype model
Null safety error on model file from quicktype model

Time:08-10

Hello this is my modal from quicktype:

// To parse this JSON data, do
//
//     final hissedetay = hissedetayFromJson(jsonString);

import 'dart:convert';

Hissedetay hissedetayFromJson(String str) => Hissedetay.fromJson(json.decode(str));

String hissedetayToJson(Hissedetay data) => json.encode(data.toJson());

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

  String code;
  Map<String, Datum> data;

  factory Hissedetay.fromJson(Map<String, dynamic> json) => Hissedetay(
    code: json["code"],
    data: Map.from(json["data"]).map((k, v) => MapEntry<String, Datum>(k, v == null ? null : Datum.fromJson(v))),
  );

  Map<String, dynamic> toJson() => {
    "code": code,
    "data": Map.from(data).map((k, v) => MapEntry<String, dynamic>(k, v == null ? null : v.toJson())),
  };
}

class Datum {
  Datum({
    required this.sembolid,
    required this.sembol,
    required this.tarih,
    required this.sektorid,
    required this.alis,
    required this.satis,
    required this.acilis,
    required this.yuksek,
    required this.yukseK1,
    required this.yukseK2,
    required this.dusuk,
    required this.dusuK1,
    required this.dusuK2,
    required this.kapanis,
    required this.kapaniS1,
    required this.kapaniS2,
    required this.hacimlot,
    required this.hacimloT1,
    required this.hacimloT2,
    required this.aort,
    required this.aorT1,
    required this.aorT2,
    required this.hacimtldun,
    required this.hacimyuzdedegisim,
    required this.hacimtl,
    required this.hacimtL1,
    required this.hacimtL2,
    required this.dunkukapanis,
    required this.oncekikapanis,
    required this.izafikapanis,
    required this.tavan,
    required this.taban,
    required this.yilyuksek,
    required this.yildusuk,
    required this.ayyuksek,
    required this.aydusuk,
    required this.haftayuksek,
    required this.haftadusuk,
    required this.oncekiyilkapanis,
    required this.oncekiaykapanis,
    required this.oncekihaftakapanis,
    required this.yilortalama,
    required this.ayortalama,
    required this.haftaortalama,
    required this.yuzdedegisimS1,
    required this.yuzdedegisimS2,
    required this.yuzdedegisim,
    required this.fiyatadimi,
    required this.kaykar,
    required this.sermaye,
    required this.saklamaor,
    required this.netkar,
    required this.net,
    required this.fiyatkaz,
    required this.piydeg,
    required this.kapanisfark,
    required this.donem,
    required this.ozsermaye,
    required this.beta,
    required this.xU100Ag,
    required this.aciklama,
  });

  int sembolid;
  String sembol;
  DateTime tarih;
  int sektorid;
  double alis;
  double satis;
  double acilis;
  double yuksek;
  double yukseK1;
  double yukseK2;
  double dusuk;
  double dusuK1;
  double dusuK2;
  double kapanis;
  double kapaniS1;
  double kapaniS2;
  int hacimlot;
  int hacimloT1;
  int hacimloT2;
  double aort;
  double aorT1;
  double aorT2;
  int hacimtldun;
  double hacimyuzdedegisim;
  int hacimtl;
  int hacimtL1;
  int hacimtL2;
  double dunkukapanis;
  double oncekikapanis;
  int izafikapanis;
  double tavan;
  double taban;
  double yilyuksek;
  double yildusuk;
  double ayyuksek;
  double aydusuk;
  double haftayuksek;
  double haftadusuk;
  double oncekiyilkapanis;
  double oncekiaykapanis;
  double oncekihaftakapanis;
  double yilortalama;
  double ayortalama;
  double haftaortalama;
  double yuzdedegisimS1;
  double yuzdedegisimS2;
  double yuzdedegisim;
  double fiyatadimi;
  int kaykar;
  int sermaye;
  double saklamaor;
  int netkar;
  double net;
  double fiyatkaz;
  double piydeg;
  dynamic kapanisfark;
  String donem;
  int ozsermaye;
  double beta;
  double xU100Ag;
  String aciklama;

  factory Datum.fromJson(Map<String, dynamic> json) => Datum(
    sembolid: json["sembolid"],
    sembol: json["sembol"],
    tarih: DateTime.parse(json["tarih"]),
    sektorid: json["sektorid"],
    alis: json["alis"].toDouble(),
    satis: json["satis"].toDouble(),
    acilis: json["acilis"].toDouble(),
    yuksek: json["yuksek"].toDouble(),
    yukseK1: json["yukseK1"].toDouble(),
    yukseK2: json["yukseK2"].toDouble(),
    dusuk: json["dusuk"].toDouble(),
    dusuK1: json["dusuK1"].toDouble(),
    dusuK2: json["dusuK2"].toDouble(),
    kapanis: json["kapanis"].toDouble(),
    kapaniS1: json["kapaniS1"].toDouble(),
    kapaniS2: json["kapaniS2"].toDouble(),
    hacimlot: json["hacimlot"],
    hacimloT1: json["hacimloT1"],
    hacimloT2: json["hacimloT2"],
    aort: json["aort"].toDouble(),
    aorT1: json["aorT1"].toDouble(),
    aorT2: json["aorT2"].toDouble(),
    hacimtldun: json["hacimtldun"],
    hacimyuzdedegisim: json["hacimyuzdedegisim"].toDouble(),
    hacimtl: json["hacimtl"],
    hacimtL1: json["hacimtL1"],
    hacimtL2: json["hacimtL2"],
    dunkukapanis: json["dunkukapanis"].toDouble(),
    oncekikapanis: json["oncekikapanis"].toDouble(),
    izafikapanis: json["izafikapanis"],
    tavan: json["tavan"].toDouble(),
    taban: json["taban"].toDouble(),
    yilyuksek: json["yilyuksek"].toDouble(),
    yildusuk: json["yildusuk"].toDouble(),
    ayyuksek: json["ayyuksek"].toDouble(),
    aydusuk: json["aydusuk"].toDouble(),
    haftayuksek: json["haftayuksek"].toDouble(),
    haftadusuk: json["haftadusuk"].toDouble(),
    oncekiyilkapanis: json["oncekiyilkapanis"].toDouble(),
    oncekiaykapanis: json["oncekiaykapanis"].toDouble(),
    oncekihaftakapanis: json["oncekihaftakapanis"].toDouble(),
    yilortalama: json["yilortalama"].toDouble(),
    ayortalama: json["ayortalama"].toDouble(),
    haftaortalama: json["haftaortalama"].toDouble(),
    yuzdedegisimS1: json["yuzdedegisimS1"].toDouble(),
    yuzdedegisimS2: json["yuzdedegisimS2"].toDouble(),
    yuzdedegisim: json["yuzdedegisim"].toDouble(),
    fiyatadimi: json["fiyatadimi"].toDouble(),
    kaykar: json["kaykar"],
    sermaye: json["sermaye"],
    saklamaor: json["saklamaor"].toDouble(),
    netkar: json["netkar"],
    net: json["net"].toDouble(),
    fiyatkaz: json["fiyatkaz"].toDouble(),
    piydeg: json["piydeg"].toDouble(),
    kapanisfark: json["kapanisfark"],
    donem: json["donem"],
    ozsermaye: json["ozsermaye"],
    beta: json["beta"].toDouble(),
    xU100Ag: json["xU100AG"].toDouble(),
    aciklama: json["aciklama"],
  );

  Map<String, dynamic> toJson() => {
    "sembolid": sembolid,
    "sembol": sembol,
    "tarih": tarih.toIso8601String(),
    "sektorid": sektorid,
    "alis": alis,
    "satis": satis,
    "acilis": acilis,
    "yuksek": yuksek,
    "yukseK1": yukseK1,
    "yukseK2": yukseK2,
    "dusuk": dusuk,
    "dusuK1": dusuK1,
    "dusuK2": dusuK2,
    "kapanis": kapanis,
    "kapaniS1": kapaniS1,
    "kapaniS2": kapaniS2,
    "hacimlot": hacimlot,
    "hacimloT1": hacimloT1,
    "hacimloT2": hacimloT2,
    "aort": aort,
    "aorT1": aorT1,
    "aorT2": aorT2,
    "hacimtldun": hacimtldun,
    "hacimyuzdedegisim": hacimyuzdedegisim,
    "hacimtl": hacimtl,
    "hacimtL1": hacimtL1,
    "hacimtL2": hacimtL2,
    "dunkukapanis": dunkukapanis,
    "oncekikapanis": oncekikapanis,
    "izafikapanis": izafikapanis,
    "tavan": tavan,
    "taban": taban,
    "yilyuksek": yilyuksek,
    "yildusuk": yildusuk,
    "ayyuksek": ayyuksek,
    "aydusuk": aydusuk,
    "haftayuksek": haftayuksek,
    "haftadusuk": haftadusuk,
    "oncekiyilkapanis": oncekiyilkapanis,
    "oncekiaykapanis": oncekiaykapanis,
    "oncekihaftakapanis": oncekihaftakapanis,
    "yilortalama": yilortalama,
    "ayortalama": ayortalama,
    "haftaortalama": haftaortalama,
    "yuzdedegisimS1": yuzdedegisimS1,
    "yuzdedegisimS2": yuzdedegisimS2,
    "yuzdedegisim": yuzdedegisim,
    "fiyatadimi": fiyatadimi,
    "kaykar": kaykar,
    "sermaye": sermaye,
    "saklamaor": saklamaor,
    "netkar": netkar,
    "net": net,
    "fiyatkaz": fiyatkaz,
    "piydeg": piydeg,
    "kapanisfark": kapanisfark,
    "donem": donem,
    "ozsermaye": ozsermaye,
    "beta": beta,
    "xU100AG": xU100Ag,
    "aciklama": aciklama,
  };
}
"required" before "this." are added by me. I'm getting this error

error: The argument type 'Datum?' can't be assigned to the parameter type 'Datum'. (argument_type_not_assignable at [myappp] lib/models/apis/hissedetay.dart:22)

How can I fix this? thanks for your help.

CodePudding user response:

  // on line 18: 
  Map<String, Datum?> data; // Datum? 

  // on line 22
    data: Map.from(json["data"]).map((k, v) => MapEntry<String, Datum?>(k, v == null ? null : Datum.fromJson(v))),
  ); // Datum? 

CodePudding user response:

Making Map<String, Datum?> data; can directly done without null check

data: Map.from(json["data"])
          .map((k, v) => MapEntry<String, Datum?>(k, Datum.fromJson(v)))

CodePudding user response:

For those having null safety problem at quicktype, just use https://javiercbk.github.io/json_to_dart/ which creates model with null safety.

  • Related