I'm migrating my Flutter 3 app over to using the new enhanced enums instead of extending the enums manually.
I have this enum (lots of the functions are obsolete now but they are used in my existing code). I json serialise it and store it.
import 'package:vepo/src/application/shared_with_back_end/option/option.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
enum VgnItmType {
nullObject(value: Option(name: 'Null Object', iconCodePoint: 0xf07a, id: 0)),
groceryItm(value: Option(name: 'Grocery Item', iconCodePoint: 0xf291, id: 1)),
menuItm(value: Option(name: 'Menu Item', iconCodePoint: 0xf2e3, id: 2)),
groceryStoreItm(
value: Option(name: 'Grocery Store', iconCodePoint: 0xf07a, id: 3)),
restaurantItm(
value: Option(name: 'Restaurant', iconCodePoint: 0xf2e7, id: 4)),
eventItm(value: Option(name: 'Event', iconCodePoint: 0xf500, id: 5)),
fashionItm(value: Option(name: 'Fashion Item', iconCodePoint: 0xf553, id: 6)),
recipeItm(value: Option(name: 'Recipe', iconCodePoint: 0xf86b, id: 7));
const VgnItmType({required this.value});
final Option value;
static Map<VgnItmType, Option> items = <VgnItmType, Option>{
VgnItmType.groceryItm: Option(
name: 'Grocery Item',
id: VgnItmType.groceryItm.index,
iconCodePoint: 0xf291),
VgnItmType.fashionItm: Option(
name: 'Fashion Item',
id: VgnItmType.fashionItm.index,
iconCodePoint: 0xf553),
VgnItmType.menuItm: Option(
name: 'Menu Item', id: VgnItmType.menuItm.index, iconCodePoint: 0xf2e3),
VgnItmType.recipeItm: Option(
name: 'Recipe', id: VgnItmType.recipeItm.index, iconCodePoint: 0xf86b),
VgnItmType.groceryStoreItm: Option(
name: 'Grocery Store',
id: VgnItmType.groceryStoreItm.index,
iconCodePoint: 0xf07a),
VgnItmType.restaurantItm: Option(
name: 'Restaurant',
id: VgnItmType.restaurantItm.index,
iconCodePoint: 0xf2e7),
VgnItmType.eventItm: Option(
name: 'Event', id: VgnItmType.eventItm.index, iconCodePoint: 0xf500),
VgnItmType.nullObject:
Option(name: '', id: VgnItmType.nullObject.index, iconCodePoint: 0xf07a)
};
String get name => items[this]!.name;
int get id => items[this]!.id;
int get iconCodePoint => items[this]!.iconCodePoint;
Option get option => items[this]!;
static List<Option> get options =>
values.map((e) => e.value).sortedBy((option) => option.name);
static List<VgnItmType> get enums =>
items.keys.toList().sortedBy((option) => option.name);
static VgnItmType idToEnum(int id) =>
items.keys.firstWhere((element) => element.id == id);
}
the Option class:
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:vepo/src/_common/entity_types/entity.dart';
part 'option.freezed.dart';
part 'option.g.dart';
@freezed
class Option extends Entity with _$Option {
const factory Option(
{required int iconCodePoint,
String? fontFamily,
required int id,
required String name}) = _Option;
const Option._();
factory Option.empty() {
return const Option(iconCodePoint: 0, id: 0, name: '');
}
factory Option.fromJson(Map<String, dynamic> json) => _$OptionFromJson(json);
}
Entity:
abstract class Entity {
const Entity();
}
I'm getting errors:
lib/src/_common/enums/vegan_item_type.dart:5:21: Error: Non-redirecting const factory invocation is not a constant expression. nullObject(value: Option(name: 'Null Object', iconCodePoint: 0xf07a, id: 0)), ^ lib/src/_common/enums/vegan_item_type.dart:6:21: Error: Non-redirecting const factory invocation is not a constant expression. groceryItm(value: Option(name: 'Grocery Item', iconCodePoint: 0xf291, id: 1)), ^ lib/src/_common/enums/vegan_item_type.dart:14:20: Error: Non-redirecting const factory invocation is not a constant expression. recipeItm(value: Option(name: 'Recipe', iconCodePoint: 0xf86b, id: 7)); ^ lib/src/_common/enums/vegan_item_type.dart:13:21: Error: Non-redirecting const factory invocation is not a constant expression. fashionItm(value: Option(name: 'Fashion Item', iconCodePoint: 0xf553, id: 6)), ^ lib/src/_common/enums/vegan_item_type.dart:9:14: Error: Non-redirecting const factory invocation is not a constant expression. value: Option(name: 'Grocery Store', iconCodePoint: 0xf07a, id: 3)), ^ lib/src/_common/enums/vegan_item_type.dart:11:14: Error: Non-redirecting const factory invocation is not a constant expression. value: Option(name: 'Restaurant', iconCodePoint: 0xf2e7, id: 4)), ^ lib/src/_common/enums/vegan_item_type.dart:7:18: Error: Non-redirecting const factory invocation is not a constant expression. menuItm(value: Option(name: 'Menu Item', iconCodePoint: 0xf2e3, id: 2)), ^ lib/src/_common/enums/vegan_item_type.dart:12:19: Error: Non-redirecting const factory invocation is not a constant expression. eventItm(value: Option(name: 'Event', iconCodePoint: 0xf500, id: 5)), ^ Failed to package /Users/benjaminfarquhar/dev/vepo_front_end/ios/... Command PhaseScriptExecution failed with a nonzero exit code
The error is here:
Isn't my Option
initialisation a constant expression?