4

I am trying to use freezed on my application, So I added

environment:
  sdk: ">=2.7.0<3.0.0"

  freezed: ^0.14.0
  freezed_annotation: ^0.14.0

to my dependencies.

dev_dependencies:
  freezed_annotation: ^0.12.0
  build_runner:  ^2.0.1
  retrofit_generator:
  flutter_localizations:
    sdk: flutter
  flutter_test:
    sdk: flutter

After that I create simple freezed class :

import 'package:freezed_annotation/freezed_annotation.dart';

part 'chart_daily_expanded_detailes_model.freezed.dart';

@freezed
abstract class ChartDailyExpandedDetailesModel
    with _$ChartDailyExpandedDetailesModel {
  const factory ChartDailyExpandedDetailesModel({
    String title,
    String testNumber,
    String testPercentage,
    String icon,
  }) = _ChartDailyExpandedDetailesModel;
}

But I got this error:

[SEVERE] freezed:freezed on lib/new_develop/model/test/chart_daily_expanded_detailes_model.dart:

The parameter `title` of `ChartDailyExpandedDetailesModel` is non-nullable but is neither required nor marked with @Default
package:app/new_develop/model/test/chart_daily_expanded_detailes_model.dart:9:12
  ╷
9 │     String title,

this is flutter doctor:

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.2.3, on Linux, locale en_US.UTF-8)
[!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/linux#android-setup for more details.
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.2)
[✓] Android Studio
[✓] VS Code (version 1.60.0)
[✓] Connected device (2 available)

! Doctor found issues in 1 category.

My project is not null safety and I want to use freezed in non null safety state.

Cyrus the Great
  • 5,145
  • 5
  • 68
  • 149
  • Well it seems that your project IS null-safety. If you change it to `String? title;` that error will go away. But then I predict that `testNumber`, `testPercentage` and `icon` will all receive the same error and can be fixed the same way. Or you add a default, non-null value for each one: `String title = ""`; – daddygames Sep 07 '21 at 13:50
  • My project is not null safety @daddygames – Cyrus the Great Sep 07 '21 at 14:16

1 Answers1

5

Add required before items, or you can make it nullable using String? or provide default value like @Default("") String icon,


@freezed
abstract class ChartDailyExpandedDetailesModel
    with _$ChartDailyExpandedDetailesModel {
  const factory ChartDailyExpandedDetailesModel({
    required String title,
    required String testNumber,
     String? testPercentage, //nullable item
    @Default("") String icon, //contain default value
  }) = _ChartDailyExpandedDetailesModel;
}

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56