Reputation: 5932
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.
Upvotes: 5
Views: 8242
Reputation: 1184
This would give me the same error:
const factory FamilyModel(
int page,
int per_page,
int pages,
int total_items,
@Default([]) List<FamilyMemberModel> familyMembers,
) = _FamilyModel;
But in this way I fixed it:
const factory FamilyModel(
int page,
int per_page,
int pages,
int total_items, {
@Default([]) List<FamilyMemberModel> familyMembers,
}) = _FamilyModel;
In substance, the @Default param must be in between the { }
so that the compiler knows that it's optional and (in that case) can be replaced by @Default([])
Upvotes: 0
Reputation: 63569
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;
}
Upvotes: 7