vnum 1.2.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • new59

Vnum #

Vnum or Value based Enum is a library that enables enum behaviour in your code using classes in flutter with more fun!.

Some highlighted features:

  • Easy define and use your cases
  • Memory Efficient
  • Supports Serialization / Deserialization
  • Support for functions inside your Vnums

Usage: #

@VnumDefinition
class CarType extends Vnum<String> {
  /// Cases
  static const CarType sedan = const CarType.define("sedan-value");
  static const CarType suv = const CarType.define("suv-value");
  static const CarType truck = const CarType.define("truck-value");
  static const CarType none = const CarType.define("unknown");

  /// Constructors
  const CarType.define(String fromValue) : super.define(fromValue);
  factory CarType(String value) => Vnum.fromValue(value,CarType);

  /// (optional) Add these constructors if serilization is supported
  dynamic toJson() => this.value;
  factory CarType.fromJson(dynamic json) => CarType(json);

  /// Extend your Vnums
  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";
  }
}
var car = CarType.sedan;
var carValue = car.value;
var carFromValue = CarType('suv-value');
var nonExisting = CarType('rocket') /// returns null

/// Vnum functions
var color = car.color() /// returns "Green"

/// Iterating cases
var allCases = Vnum.allCasesFor(CarType);
print(allCases.length); /// prints 4

Comparison: #

var sedan = CarType.sedan;
var truck = CarType.truck
print(sedan == truck);

Class member usage: #

@JsonSerializable()
class SampleResponse {
  
  @JsonKey(name: "carType")
  CarType carType;

  SampleResponse();
  factory SampleResponse.fromJson(Map<String, dynamic> json) =>
      _$SampleResponseFromJson(json);
  Map<String, dynamic> toJson() => _$SampleResponseToJson(this);
}

Deserializiation: #

final jsonString = '{"carType":"suv-value"}';

/// Deserialize Json String
var decodedData = json.decode(jsonString);
SampleResponse response = SampleResponse.fromJson(decodedData);

print(response.carType); /// CarType.suv;

Serialization: #

var serialized = json.encode(response);
print(serialized); /// prints {"carType":"suv-value"}

#

Installation: #

Add following dependencies in your pubspec.yaml file:

dependencies:
  flutter:
    sdk: flutter

dev_dependencies:
  flutter_test:
    sdk: flutter
  build_runner: ^1.0.0
  reflectable: ^2.0.10+1
  json_serializable: ^2.0.0

Add a file build.yaml to your project's root folder or update the extisting file with the following code to your build.

targets:
  $default:
    builders:
      reflectable:
        generate_for:
          - lib/main.dart
          - test/**_test.dart
        options:
          formatted: true

Add following lines to your main.dart add the following imports:

import 'main.reflectable.dart';
import 'package:reflectable/reflectable.dart';

void main() {
  initializeReflectable();
}

if you run packages pub run build_runner build, you should have no build problems by now and your project is ready for using Vnum!

Definition: #

Define your Vnum with value of T type with following steps:

  • Import Vnum reference package:Vnum/Vnum.dart

  • Define your class which extends Vnum<T>

  • Add @VnumDefinition attribute to your class

  • Add public constructors

  • Define your cases as static const with the same type of your Vnum

  • If you need serialization support for your Vnum add toJson() and fromJson() constructor to your Vnum definition

import 'package:Vnum/Vnum.dart';

@VnumDefinition
class MyEnum extends Vnum<T> {
/// Case Definition
static const MyEnum case1 = const MyEnum.define(value1);
static const MyEnum case2 = const MyEnum.define(value2);

/// Used for defining cases
MyEnum.define(String fromValue) : super.define(fromValue);

/// Used for loading enum using value
factory MyEnum(String value) => Vnum.fromValue(value,MyEnum);

/// (optional) Support for serialization/deserialization
dynamic toJson() => this.value;
factory MyEnum.fromJson(dynamic json) => MyEnum(json);
}

Generate codes: #

Vnum is based on Dart Reflection and requires generating codes:

Use the following command in your terminal to generate reflection codes for your project


Reflectable documentation: https://github.com/dart-lang/reflectable


[1.1.1] - Performance improvement

  • Add allCasesFor(<Type>) for iterating Vnums
  • Update documentation

[1.1.0] - Serialization Support

  • Serialization support has been added
  • Update documentation

[1.0.2] - Unify keywords

  • Update documentation

[1.0.1] - Update documentation

  • Update documentation
  • Add example folder

[1.0.0] - Initial release

  • First stable release.

example/example.dart

import 'dart:convert';

import 'package:json_annotation/json_annotation.dart';
import 'package:vnum/vnum.dart';
import 'example-types.dart';
part 'example.g.dart';

/// This is example use of Vnum in dart, initializeReflectable()
/// add  initializeReflectable() the main() of your application
/// if you are getting a undefined_function error, run
/// flutter packages pub run build_runner build example --delete-conflicting-outputs

/// Usage: Example
void example() {


  var scanType = CarType.sedan;
  var scanType2 = CarType.suv;
  var scanType3 = CarType.sedan;

  print(scanType.value); /// prints sedan-value
  print(scanType == scanType2); /// prints false
  print(scanType == scanType3); /// prints true

  var scanTypeFetched = CarType("sedan-value");
  print(scanTypeFetched.value); /// prints sedan-value
  print(scanTypeFetched == CarType.sedan); /// prints true

  var unknownFruit = Fruit(99);
  print(unknownFruit == null); /// prints true

  /// Advanced Use
  var fruit = Fruit(3);
  print("banana has ${fruit.color()} color"); /// prints banana has Yellow color

  /// Iterating cases
  var allCases = Vnum.allCasesFor(CarType);
  print(allCases.length); /// prints 4


}

/// Serialization Example
void serializationExample() {
   /// Mock Response
   final jsonString = '{"carType":"suv-value"}';

   /// Decode
   var decodedData = json.decode(jsonString);
   SampleResponse response = SampleResponse.fromJson(decodedData);

   /// Verify
   print(response.carType); /// CarType.suv;

   /// Serialize again
   var serialized = json.encode(response);

   /// Make sure generated string is the same as original
   print(serialized); // {"carType":"suv-value"}
}


@JsonSerializable()
class SampleResponse {
  
  @JsonKey(name: "carType")
  CarType carType;

  SampleResponse();
  factory SampleResponse.fromJson(Map<String, dynamic> json) =>
      _$SampleResponseFromJson(json);
  Map<String, dynamic> toJson() => _$SampleResponseToJson(this);
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  vnum: ^1.2.0

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

Alternatively, your editor might support flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:vnum/vnum.dart';
  
Version Uploaded Documentation Archive
1.2.0 Apr 10, 2019 Go to the documentation of vnum 1.2.0 Download vnum 1.2.0 archive
1.1.0 Apr 10, 2019 Go to the documentation of vnum 1.1.0 Download vnum 1.1.0 archive
1.0.2 Apr 10, 2019 Go to the documentation of vnum 1.0.2 Download vnum 1.0.2 archive
1.0.1 Apr 10, 2019 Go to the documentation of vnum 1.0.1 Download vnum 1.0.1 archive
1.0.0 Apr 9, 2019 Go to the documentation of vnum 1.0.0 Download vnum 1.0.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
18
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
59
Learn more about scoring.

We analyzed this package on Apr 22, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.2.0
  • pana: 0.12.14
  • Flutter: 1.4.7

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Format lib/vnum.dart.

Run flutter format to format lib/vnum.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
json_annotation ^2.0.0 2.2.0
json_serializable ^2.0.0 2.2.1
reflectable ^2.0.10+1 2.0.10+1
Transitive dependencies
analyzer 0.34.3 0.36.1
args 1.5.1
async 2.2.0
build 1.1.0 1.1.3
build_config 0.3.2
build_daemon 0.2.3 0.5.0
build_resolvers 0.2.3 1.0.4
build_runner_core 2.0.1 3.0.3
built_collection 4.2.0
built_value 6.4.0
charcode 1.1.2
code_builder 3.2.0
collection 1.14.11
convert 2.1.1
crypto 2.0.6
csslib 0.15.0
dart_style 1.2.3 1.2.7
fixnum 0.10.9
front_end 0.1.9+1 0.1.16
glob 1.1.7
graphs 0.2.0
html 0.14.0+1
http 0.12.0+2
http_multi_server 2.0.5
http_parser 3.1.3
io 0.3.3
js 0.6.1+1
kernel 0.3.9+1 0.3.16
logging 0.11.3+2
matcher 0.12.5
meta 1.1.6 1.1.7
mime 0.9.6+2
package_config 1.0.5
package_resolver 1.0.10
path 1.6.2
pedantic 1.5.0
plugin 0.2.0+3
pool 1.4.0
pub_semver 1.4.2
pubspec_parse 0.1.4
quiver 2.0.3
shelf 0.7.5
shelf_web_socket 0.2.3
sky_engine 0.0.99
source_gen 0.9.4+1 0.9.4+2
source_span 1.5.5
stack_trace 1.9.3
stream_channel 2.0.0
stream_transform 0.0.17
string_scanner 1.0.4
term_glyph 1.1.0
timing 0.1.1+1
typed_data 1.1.6
vector_math 2.0.8
watcher 0.9.7+10
web_socket_channel 1.0.12
yaml 2.1.15
Dev dependencies
build_runner ^1.0.0 1.2.3 1.3.3
flutter_test