quicktype icon indicating copy to clipboard operation
quicktype copied to clipboard

Add Dart non-nullable support

Open jarekb123 opened this issue 3 years ago • 26 comments

As Dart non-nullable is stable now, please add option to generate non-nullable classes with the tool (especially freezed classes)

jarekb123 avatar Mar 07 '21 17:03 jarekb123

+1 we really need this feature.

xamantra avatar Mar 10 '21 13:03 xamantra

It looks to me like that all that is needed for null safety is to make it work exactly like make all fields required, but to use just 'required' as the filed prefix, instead of '@required'. It should probably be under its own switch so that you keep the old functionality.

chrislaurie-io avatar Mar 11 '21 10:03 chrislaurie-io

Also the tool should detect if some field may be nullable (eg. If some field has null value in provided json).

jarekb123 avatar Mar 11 '21 11:03 jarekb123

Think about copyWith(...). The dart migration tool treats the copyWith parameters as non-nullable. I had to manually put question marks to each of the parameters (sorry I'm bad at regex). All parameters in the copyWith method should be nullable to properly implement the prop: prop ?? this.prop expression.

It shouldn't even be an option. Quicktype must generate copyWith with all parameters as nullable. With ?

image

Imagine a copyWith method with all parameters as required (non-nullable). That would be a disaster.

xamantra avatar Mar 11 '21 11:03 xamantra

This is what freezed null-safely generates:

class _$ShopCopyWithImpl<$Res> implements $ShopCopyWith<$Res> {
  _$ShopCopyWithImpl(this._value, this._then);

  final Shop _value;
  // ignore: unused_field
  final $Res Function(Shop) _then;

  @override
  $Res call({
    Object? shopCode = freezed,
    Object? shopName = freezed,
  }) {
    return _then(_value.copyWith(
      shopCode: shopCode == freezed
          ? _value.shopCode
          : shopCode // ignore: cast_nullable_to_non_nullable
              as String,
      shopName: shopName == freezed
          ? _value.shopName
          : shopName // ignore: cast_nullable_to_non_nullable
              as String,	
    ));
  }
}


chrislaurie-io avatar Mar 11 '21 11:03 chrislaurie-io

dart code generation packages.. stopped using since 2019. especially with localizations. I just personally hate it. the explanation would be too long. It's based on experience. also, not all people use freezed or any code generation dart packages.

xamantra avatar Mar 11 '21 14:03 xamantra

+1 We really need to generate dart non-nullable classes with quicktype!

TheWCKD avatar Mar 31 '21 13:03 TheWCKD

It looks to me like that all that is needed for null safety is to make it work exactly like make all fields required, but to use just 'required' as the filed prefix, instead of '@required'. It should probably be under its own switch so that you keep the old functionality.

Also, when the JSON fields are optional the generated class fields should be nullable with ? suffix, and references to the fields (after null check) should assert non-null using !, for example:

class Sensordata {
...
  String? sensor;
  List<Datum>? data;
...
  Map<String, dynamic> toJson() => {
        "sensor": sensor == null ? null : sensor,
        "data": data == null
            ? null
            : List<dynamic>.from(data!.map((x) => x.toJson())),
      };
}

class Datum {
...
  String? value;
  DateTime? readingTime1;
...
  Map<String, dynamic> toJson() => {
        "value": value == null ? null : value,
        "reading_time1": readingTime1 == null
            ? null
            : "${readingTime1!.year.toString().padLeft(4, '0')}-${readingTime1!.month.toString().padLeft(2, '0')}-${readingTime1!.day.toString().padLeft(2, '0')}",
      };
}

pohara60 avatar May 11 '21 07:05 pohara60

+1

xang555 avatar Aug 17 '21 05:08 xang555

+1

nimi0112 avatar Aug 22 '21 05:08 nimi0112

+1

oliverbytes avatar Oct 18 '21 12:10 oliverbytes

+1

RLZW avatar Nov 06 '21 23:11 RLZW

https://github.com/chunlee-thong/dart-quicktype Give this a try if it fits your use case

chunlee-thong avatar Jan 03 '22 10:01 chunlee-thong

Still not supported yet? Oh my God!

Kakukaho avatar Feb 18 '22 09:02 Kakukaho

The right one should be like this:#1898

RmondJone avatar Mar 03 '22 06:03 RmondJone

可以看看我提交的PR,现在等待官方合并,#1904 You can take a look at the PR I submitted, now waiting for the official merge。#1904

RmondJone avatar Mar 11 '22 16:03 RmondJone

https://github.com/chunlee-thong/dart-quicktype/tree/dev 如果它适合您的用例,请尝试一下 Good job. That's what I need.Thank you

ThinkerJack avatar Mar 14 '22 06:03 ThinkerJack

https://github.com/chunlee-thong/dart-quicktype/tree/dev Good job. That's what I need. Thank you @chunlee-thong

truongthi93 avatar Mar 30 '22 04:03 truongthi93

we really need this feature.when will change?

CCHenry avatar Apr 26 '22 05:04 CCHenry

I was also suprised to find, that quicktype did not adapt their tool for a year now. Instead of just replacing the @required by required, you removed it entirely, while ignoring that this is also invalid dart code, when sound null-safety is enabled? Right now, the dart generation is almost unusable, since pretty much every dart code was moved to sound null safety in this time...

khainke avatar Jun 15 '22 12:06 khainke

I would also love some null-safety support, however there is a workaround: you can add // @dart=2.9 at the top of your generated file, then you will be able to use it in your project (Note: you will still need to run and build your app with the --no-sound-null-safety check and null checks won't properly work on your generated types).

geiszla avatar Jun 15 '22 13:06 geiszla

I would also love some null-safety support, however there is a workaround: you can add // @dart=2.9 at the top of your generated file, then you will be able to use it in your project (Note: you will still need to run and build your app with the --no-sound-null-safety check and null checks won't properly work on your generated types).

I dont think this is a valid option. In dart, one would usually use generated files as part to add additional functionality. For this one would have to migrate the whole project. Besides, after a year of stable sound null-safety for dart, this should be properly supported, as the default.

khainke avatar Jun 15 '22 13:06 khainke

I would also love some null-safety support, however there is a workaround: you can add // @dart=2.9 at the top of your generated file, then you will be able to use it in your project (Note: you will still need to run and build your app with the --no-sound-null-safety check and null checks won't properly work on your generated types).

I dont think this is a valid option. In dart, one would usually use generated files as part to add additional functionality. For this one would have to migrate the whole project. Besides, after a year of stable sound null-safety for dart, this should be properly supported, as the default.

Yes, I agree, this is not a solution, just a workaround and this should be fixed, however this project doesn't seem to be maintained anymore and I don't know of any alternative, so it's the only thing we have.

Also, I don't know what you mean by "migrate the whole project". Those 2 changes (adding // @dart=2.9 comment to your types file and adding --no-sound-null-safety flag to your scripts) are the only ones needed for this to (somewhat) work.

geiszla avatar Jun 15 '22 13:06 geiszla

Also, I don't know what you mean by "migrate the whole project". Those 2 changes (adding // @dart=2.9 comment to your types file and adding --no-sound-null-safety flag to your scripts) are the only ones needed for this to (somewhat) work.

And to the toolsets that use your dart code (flutter) and to the projects that want to use your library or any other place that has an import. You essentially migrate your project to a Dart 2.10 project with the --no-sound-null-safety flag.

There's a reason why pub.dev has a chip-indicator/flag for libraries with sound null safety, you generally dont wanna mix the two. and 95% of dart code currently written will want to support sound null safety.

khainke avatar Jun 16 '22 08:06 khainke

And to the toolsets that use your dart code (flutter) and to the projects that want to use your library or any other place that has an import.

Ahh yeah, you're right, it's probably not a good workaround for libraries, only applications.

geiszla avatar Jun 16 '22 08:06 geiszla

why is the dart community so vehemently ignored in this project? its disheartening.

Well, comments like these aren't going to help

anjared avatar Jul 02 '22 19:07 anjared

why is the dart community so vehemently ignored in this project? its disheartening.

Well, comments like these aren't going to help

Disagree. People should be aware that Quicktype as a whole is practically abandonware at this point. While everyone should be grateful for the free, open-sourced code, it's still polite for the authors to mark a project as unmaintained/archived, so people don't create a dependency on it if they're not expecting obvious issues like these.

I was someone who didn't notice, created a dependency on it, and am now spending my time trying to patch it instead of working on my projects.

thecatanon avatar Nov 22 '22 18:11 thecatanon