-
-
Notifications
You must be signed in to change notification settings - Fork 249
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
@Default using non literal const (like Color) #149
Comments
The problem is that jsonserializable doesn't like Color as default value I can't so much for that. It's jsonserializable the problem, not freezed |
I thing I found a way to achieve it, not the best one, but it should work. import 'package:flutter/material.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:flutter/foundation.dart';
import '../../converter/color_converter.dart';
part 'themable.freezed.dart';
part 'themable.g.dart';
@freezed
abstract class Themable with _$Themable {
const factory Themable({
@ColorIntConv()
@Default(ColorIntConv.defaultColor)
@JsonKey()
Color backgroundColorRGB,
}) = _Themable;
factory Themable.fromJson(Map<String, dynamic> json) =>
_$ThemableFromJson(json);
}
@default(ColorIntConv.defaultColor) for Freezed. Change the converter to handle default value if null : import 'package:flutter/widgets.dart';
import 'package:json_annotation/json_annotation.dart';
class ColorIntConv implements JsonConverter<Color, int> {
const ColorIntConv();
static const Color defaultColor = Color.fromRGBO(0, 0, 0, 0);
@override
Color fromJson(int json) => json == null ? defaultColor : Color(json);
@override
int toJson(Color object) => object == null ? defaultColor : object.value;
} No the most concise way but at least it works. Should works with #82 too. I close it then, thanks for pointing me out to jsonserializable. 😃 |
Hi,
I run into an issue, trying to give a default value to a Color:
The json_converter is the following :
This give the error :
By removing the @default, it generates fine (themable.g.dart):
So my take is that by handling those both json_converter et default annotation at the same time, it would be possible to handle Color or even custom type, assuming converter is provided.
What do you think ?
This could also help with : #82
The text was updated successfully, but these errors were encountered: