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

Arguments of a constant creation must be constant expressions. When adding custom converter with json_serializable #1080

Open
abikko opened this issue May 2, 2024 · 2 comments
Assignees
Labels
bug Something isn't working needs triage

Comments

@abikko
Copy link

abikko commented May 2, 2024

Describe the bug
Hello. When I am generating freezed class with custom converters for fields in freezed class I am getting error

Analyzer error:
const_with_non_constant_argument

Arguments of a constant creation must be constant expressions.

To Reproduce

@freezed
class TimeslotForTheDay with _$TimeslotForTheDay {
  const TimeslotForTheDay._();

  factory TimeslotForTheDay({
    required String id,
    @JsonKey(name: 'planned_quantity') int? plannedQuantity,
    @JsonKey(name: 'fact_quantity') int? factQuantity,
    @JsonKey(name: 'start_time', toJson: _toJson, fromJson: _fromJson) required DateTime startTime,
    @JsonKey(name: 'end_time', toJson: _toJson, fromJson: _fromJson) required DateTime endTime,
  }) = _TimeslotForTheDay;

  static int _toJson(DateTime value) => value.millisecondsSinceEpoch;

  static DateTime _fromJson(int value) => DateTime.fromMillisecondsSinceEpoch(value);

  factory TimeslotForTheDay.fromJson(Map<String, dynamic> json) => _$TimeslotForTheDayFromJson(json);
}
@abikko abikko added bug Something isn't working needs triage labels May 2, 2024
@abikko
Copy link
Author

abikko commented May 2, 2024

Flutter doctor output:

Doctor summary (to see all details, run flutter
doctor -v):
[✓] Flutter (Channel stable, 3.19.5, on macOS 14.4 23E214 darwin-arm64, locale en-KZ)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[!] Xcode - develop for iOS and macOS (Xcode 15.3)
✗ Unable to get list of installed Simulator runtimes.
✗ CocoaPods not installed.
CocoaPods is used to retrieve the iOS and macOS platform side's plugin code that responds to your plugin usage on the Dart side.
Without CocoaPods, plugins will not work on iOS or macOS.
For more info, see https://flutter.dev/platform-plugins
To install see https://guides.cocoapods.org/using/getting-started.html#installation for instructions.
[✓] Chrome - develop for the web
[✓] Android Studio (version 2023.2)
[✓] VS Code (version 1.88.1)
[✓] Connected device (4 available)
[✓] Network resources

! Doctor found issues in 1 category.

@Pulkit0001
Copy link

@abikko can you please try with the below code as _TimesLotForTheDay is a mixin and does not have visibility with the private methods(_fromJson, _toJson) of TimesLotForTheDay class.

import 'package:freezed_annotation/freezed_annotation.dart';

part 'times_lot_for_the_day.freezed.dart';
part 'times_lot_for_the_day.g.dart';

int dateToJson(DateTime value) => value.millisecondsSinceEpoch;

DateTime dateFromJson(int value) => DateTime.fromMillisecondsSinceEpoch(value);

@freezed
class TimesLotForTheDay with _$TimesLotForTheDay {
  const TimesLotForTheDay._();

  const factory TimesLotForTheDay({
    required String id,
    @JsonKey(name: 'planned_quantity') int? plannedQuantity,
    @JsonKey(name: 'fact_quantity') int? factQuantity,
    @JsonKey(name: 'start_time', toJson: dateToJson, fromJson: dateFromJson)
    required DateTime startTime,
    @JsonKey(name: 'end_time', toJson: dateToJson, fromJson: dateFromJson)
    required DateTime endTime,
  }) = _TimesLotForTheDay;

  factory TimesLotForTheDay.fromJson(Map<String, dynamic> json) =>
      _$TimesLotForTheDayFromJson(json);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage
Projects
None yet
Development

No branches or pull requests

3 participants