Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code generation for complex types with default value produces warning #1170

Closed
guenth39 opened this issue Jul 8, 2022 · 6 comments · Fixed by #1172
Closed

Code generation for complex types with default value produces warning #1170

guenth39 opened this issue Jul 8, 2022 · 6 comments · Fixed by #1172
Assignees

Comments

@guenth39
Copy link

guenth39 commented Jul 8, 2022

When having a constructor with a not nullable parameter that is a complex type and has a default value, flutter gives me the following warning:

: Warning: Operand of null-aware operation '??' has type 'Duration' which excludes null.
lib/model/my_class.g.dart:13
 - 'Duration' is from 'dart:core'.
      d: Duration(microseconds: json['d'] as int) ?? Duration.zero,

See the following example code to reproduce it:

part 'my_class.g.dart';

@JsonSerializable()
class MyClass {
  const MyClass({
    required this.a,
    required this.c,
    this.b = '',
    this.d = Duration.zero,
  });

  factory MyClass.fromJson(Map<String, dynamic> json) =>
      _$MyClassFromJson(json);

  final String a, b;
  final Duration c, d;

  Map<String, dynamic> toJson() => _$MyClassToJson(this);
}
// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'my_class.dart';

// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************

MyClass _$MyClassFromJson(Map<String, dynamic> json) => MyClass(
      a: json['a'] as String,
      c: Duration(microseconds: json['c'] as int),
      b: json['b'] as String? ?? '',
      d: Duration(microseconds: json['d'] as int) ?? Duration.zero,
    );

Map<String, dynamic> _$MyClassToJson(MyClass instance) => <String, dynamic>{
      'a': instance.a,
      'b': instance.b,
      'c': instance.c.inMicroseconds,
      'd': instance.d.inMicroseconds,
    };

flutter --version

Flutter 3.0.2 • channel stable • https://github.com/flutter/flutter.git
Framework • revision cd41fdd495 (vor 4 Wochen) • 2022-06-08 09:52:13 -0700
Engine • revision f15f824b57
Tools • Dart 2.17.3 • DevTools 2.12.2
@guenth39 guenth39 changed the title Wrong code generation for class types with default value Code generation for complex types with default value produces warning Jul 8, 2022
@kevmoo
Copy link
Collaborator

kevmoo commented Jul 8, 2022

This is a FUN one. Duration is weird – because it can be represented as a const. So it can be used as a default value.

This is different than Uri, DateTime, etc – the other values we support. Absolutely a bug. Will look...

@kevmoo
Copy link
Collaborator

kevmoo commented Jul 8, 2022

Fix is on the way!

kevmoo added a commit that referenced this issue Jul 8, 2022
Fixes #1170

`Duration` was overlooked when supporting default values in constructors. That's now fixed!
@kevmoo
Copy link
Collaborator

kevmoo commented Jul 8, 2022

published as 6.3.1

@guenth39
Copy link
Author

Thank u for the fast fix! 🚀 👏

@AlexeyKatsuro
Copy link

Hello @kevmoo !

Is it possible to adjust codegen behaviour for Duration class and replace inMicroseconds with inSeconds and Duration(microseconds: ), with Duration(seconds: ), for whole project?

@kevmoo
Copy link
Collaborator

kevmoo commented Dec 14, 2023

Is it possible to adjust codegen behaviour for Duration class and replace inMicroseconds with inSeconds and Duration(microseconds: ), with Duration(seconds: ), for whole project?

For the whole project? I don't think so...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants