While develop with dart/Flutter, I developed certain ways of doing things. I collect them in this package and publish it for anyone who may need it.
- Enhancement for dart:convert package.
Adding package to pubspec.yaml
:
dependencies:
mikeway ^0.0.1
Import the package in the source file and you're up to go:
import 'package:mikeway/mikeway.dart';
Say you have a Person
class to encode and decode:
import 'package:mikeway/mikeway.dart';
class Person implements JsonEncodable {
final String name;
final int age;
Person(this.name, this.age);
@override
Map<String, dynamic> toJson() {
return {
'name': name,
'age': age,
};
}
factory Person.fromJson(Map<String, dynamic> map) {
return Person(map['name'], map['age']);
}
}
Here is how you encode from Person to String, and decode from String to Person object:
print(jsonEncode(Person('mike', 20), indenx: ' '));
// {
// "name": "mike",
// "age": 20
// }
final user = jsonDecode('{"name": "mike", "age": 20}', Person.fromJson);
print(user.runtimeType);
// Person
print(user.toJson());
// {name: mike, age: 20}
- Pretty print dart object.
- Wrapping Convert Object, List to JSON string and Convert/Parse JSON string, array into Object, List.
- Data transport (my unpublished dart library for data codec to reduce netwrok bandwidth).