amirkamali / flutter_vnum Goto Github PK
View Code? Open in Web Editor NEWThis class library enables to have enum-behaviour using value in Flutter
License: MIT License
This class library enables to have enum-behaviour using value in Flutter
License: MIT License
import 'main.reflectable.dart';
import 'package:reflectable/reflectable.dart';
initializeReflectable();
Build.yaml:
targets:
$default:
builders:
reflectable:
generate_for:
- lib/main.dart
- test/**_widget_test.dart
options:
formatted: true
My project is not able to resolve this step.
Could you please clarify when I need to regenerate codes with
packages pub run build_runner build --delete-conflicting-outputs
as opposed to:
packages pub run build_runner build
https://github.com/adsharma/py2many/pull/112/checks?check_run_id=2427684955
Because every version of vnum depends on flutter any from sdk which is forbidden, vnum is forbidden.
So, because tests depends on vnum any, version solving failed.
py2many is a transpiler. So we're sensitive to our CI runtimes and would like to minimize dependencies.
Specifically, would like to stick to the core language libraries instead of frameworks.
Thank you for building vnum! I couldn't get some of the python features to work with dart enums.
Needed for py2many/py2many#26
need to upgrade to json_annotion 4.0.0 to switch to null safety
Because vnum 1.2.6 depends on json_annotation ^3.0.1 and no versions of vnum match >1.2.6 <2.0.0, vnum ^1.2.6 requires json_annotation ^3.0.1.
You've got an example:
String color(){
if (value == CarType.sedan.value) {
return "Green";
}else if (value == CarType.suv.value) {
return "Orange";
}else if (value == CarType.truck.value) {
return "Yellow";
}
return "Unknown";
}
Is it possible to make color as a property of enum item and rewrite method as:
String color(){
return this._color;
}
?
Will soon serialization / deserializiation work in this case?
during vnum deserialization I get this error:
[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: Bad state: Reflectable has not been initialized.
E/flutter (17762): Please make sure that the first action taken by your program
E/flutter (17762): in `main` is to call `initializeReflectable()`.
E/flutter (17762): #0 data (package:reflectable/src/reflectable_builder_based.dart:177:5)
E/flutter (17762): #1 data (package:reflectable/src/reflectable_builder_based.dart)
E/flutter (17762): #2 ReflectableImpl.reflectType (package:reflectable/src/reflectable_builder_based.dart:2544:26)
E/flutter (17762): #3 Vnum._fetchValue (package:vnum/vnum.dart:46:42)
E/flutter (17762): #4 new Vnum.fromValue (package:vnum/vnum.dart:40:12)
my code looks like
@VnumDefinition
class OrganizationType extends Vnum<String> {
/// Constructors
const OrganizationType.define(String fromValue) : super.define(fromValue);
factory OrganizationType(String value) => Vnum.fromValue(value, OrganizationType);
factory OrganizationType.fromJson(dynamic json) => OrganizationType(json);
/// Cases
static const OrganizationType customer = OrganizationType.define('customer');
static const OrganizationType supplier = OrganizationType.define('supplier');
@override
dynamic toJson() => value;
}
what is the problem? should I add initializeReflectable() ?
VNum is very compatible with json_seriliazable but there seems to be a problem when it comes to using Vnums in freezed classes. Freezed itself uses json_serilizable. For example:
userType.dart
@VnumDefinition
class UserType extends Vnum<String> {
static const UserType superAdmin = UserType.define('super-admin');
static const UserType admin = UserType.define('admin');
static const UserType staff = UserType.define('staff');
static const UserType student = UserType.define('student');
const UserType.define(String fromValue) : super.define(fromValue);
factory UserType(String value) => Vnum.fromValue(value,UserType);
@override
String toJson() => value;
factory UserType.fromJson(String json) => UserType(json);
}
person.dart
import 'package:admin/models/userType.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
part 'person.freezed.dart';
part 'person.g.dart';
@freezed
abstract class Person implements _$Person {
factory Person(@JsonKey(name: 'userType') UserType userType, String name, String imageUrl, String thumbnail) = _Person;
factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
}
Expected Behaviour
in person.g.dart there should be appropriate deserialisation for UserType
Actual Behaviour
person.g.dart
Map<String, dynamic> _$_$_PersonToJson(_$_Person instance) => <String, dynamic>{
'userType': instance.userType,
'name': instance.name,
'imageUrl': instance.imageUrl,
'thumbnail': instance.thumbnail,
};
There is no proper deserialisation. To correctly deserialise you have to enter serialisation methods in @JsonKey
annotation like this
@JsonKey(name: 'userType', fromJson: _userTypeFromJson, toJson: _userTypeToJson) UserType userType
I get The switch case expression type 'MyVnum' can't override the == operator.
.
$ dart pub upgrade --null-safety
null-safety compatible versions do not exist for:
Can this be addressed in one of the upcoming releases? Cc: @jayvdb
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.