Brick
- [x] model
- [ ] feature_brick
- [ ] app_ui
- [ ] service
- [ ] service_package
Status
READY
Breaking Changes
NO
Description
Using the model brick to generate a model with the configuration containing nullable types produces unexpected results.
sample.config.json
{
"model_name": "sample_model",
"additionals": ["copyWith", "json"],
"style": "freezed",
"properties": [
{ "name": "id", "type": "String" },
{ "name": "content", "type": "String?" },
{ "name": "createdAt", "type": "DateTime" },
{ "name": "updatedAt", "type": "DateTime?" },
{ "name": "comments", "type": "List<String?>" },
{ "name": "sampleNulls", "type": "List<String?>?" }
]
}
The configuration file above generates the following results with freezed (Though the results are similar for other configurations)
sample_model.dart
import 'package:freezed_annotation/freezed_annotation.dart';
part 'sample_model.freezed.dart';
part 'sample_model.g.dart';
/// {@template sample_model}
/// SampleModel description
/// {@endtemplate}
@freezed
class SampleModel with _$SampleModel {
/// {@macro sample_model}
const factory SampleModel({
required String id,
required String? content,
required DateTime createdAt,
required DateTime? updatedAt,
required List<String?> comments,
required List<String?>? sampleNulls,
}) = _SampleModel;
/// Creates a SampleModel from Json map
factory SampleModel.fromJson(Map<String, dynamic> data) => _$SampleModelFromJson(data);
}
Which clearly indicates that the nullable types are still marked as required but would be better modelled as optional named parameters. Resulting in
sample_model.dart
import 'package:freezed_annotation/freezed_annotation.dart';
part 'sample_model.freezed.dart';
part 'sample_model.g.dart';
/// {@template sample_model}
/// SampleModel description
/// {@endtemplate}
@freezed
class SampleModel with _$SampleModel {
/// {@macro sample_model}
const factory SampleModel({
required String id,
String? content,
required DateTime createdAt,
DateTime? updatedAt,
required List<String?> comments,
List<String?>? sampleNulls,
}) = _SampleModel;
/// Creates a SampleModel from Json map
factory SampleModel.fromJson(Map<String, dynamic> data) => _$SampleModelFromJson(data);
}
Type of Change
- [x] β¨ New feature (non-breaking change which adds functionality)
- [ ] π οΈ Bug fix (non-breaking change which fixes an issue)
- [ ] β Breaking change (fix or feature that would cause existing functionality to change)
- [ ] π§Ή Code refactor
- [ ] β
Build configuration change
- [ ] π Documentation
- [ ] ποΈ Chore