Use freezed on class that has a Color member in it #945
-
|
I want to create a model called "Categories" and make it compatible with to/fromJson. It will have 2 fields: String name and Color color. When I use freezed, I can get the following code to generate: I have 2 problems:
code: error: If I remove the "required" in front of String name, I get the following error: Like I said, I'd like to exclude Color color from the to/fromJson but I'd like to give it a default value so that it's not optional. Any ideas? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
|
The problem is that your Do: const factory Category({required String name, ...}) = _Category;` |
Beta Was this translation helpful? Give feedback.
Don't specify the
requiredkeyword then. The error you shared initially has nothing to do withColoror Freezed. It's simply not valid Dart codeAs for
fromJson, Color is not serializable. If you want a from/toJson, you'll need a custom JsonConverter