vnum 1.2.6 copy "vnum: ^1.2.6" to clipboard
vnum: ^1.2.6 copied to clipboard

discontinued

A library that enables value based enum behaviour using classes in flutter.

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);
}
12
likes
40
pub points
31%
popularity

Publisher

unverified uploader

A library that enables value based enum behaviour using classes in flutter.

Repository (GitHub)
View/report issues

License

MIT (LICENSE)

Dependencies

flutter, json_annotation, reflectable

More

Packages that depend on vnum