Last active
April 16, 2020 05:55
-
-
Save oligazar/83df0a1828433d8af06dc8bcaa2be7ab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library built_vehicle; | |
import 'dart:convert'; | |
import 'package:built_collection/built_collection.dart'; | |
import 'package:built_value/built_value.dart'; | |
import 'package:built_value/serializer.dart'; | |
import 'package:clean_architecture/built_value/serializers.dart'; | |
part 'built_vehicle.g.dart'; | |
// flutter pub run build_runner build | |
// flutter pub run build_runner watch | |
abstract class BuiltVehicle implements Built<BuiltVehicle, BuiltVehicleBuilder> { | |
VehicleType get type; | |
String get brand; | |
int get price; | |
@nullable | |
bool get nullableValue; | |
BuiltList<String> get passengers; | |
BuiltVehicle._(); | |
factory BuiltVehicle([updates(BuiltVehicleBuilder b)]) = _$BuiltVehicle; | |
String toJson() { | |
return json.encode(serializers.serializeWith(BuiltVehicle.serializer, this)); | |
} | |
static BuiltVehicle fromJson(String jsonString) { | |
return serializers.deserializeWith(BuiltVehicle.serializer, json.decode(jsonString)); | |
} | |
// to make BuiltVehicle serializable add this | |
static Serializer<BuiltVehicle> get serializer => _$builtVehicleSerializer; | |
} | |
class VehicleType extends EnumClass { | |
// fields go here | |
static const VehicleType car = _$car; | |
static const VehicleType plane = _$plane; | |
static const VehicleType tractor = _$tractor; | |
static const VehicleType boat = _$boat; | |
const VehicleType._(String name): super(name); | |
static BuiltSet<VehicleType> get values => _$values; | |
static VehicleType valueOf(String name) => _$value(name); | |
static Serializer<VehicleType> get serializer => _$vehicleTypeSerializer; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library serializers; | |
import 'dart:convert'; | |
import 'package:built_collection/built_collection.dart'; | |
import 'package:built_value/built_value.dart'; | |
import 'package:built_value/serializer.dart'; | |
import 'package:built_value/standard_json_plugin.dart'; | |
import 'package:clean_architecture/built_value/built_vehicle.dart'; | |
import 'package:clean_architecture/convoy_model/convoy_model.dart'; | |
part 'serializers.g.dart'; | |
@SerializersFor(const [ | |
BuiltVehicle, | |
VehicleType, | |
ConvoyModel, | |
]) | |
final Serializers serializers = (_$serializers.toBuilder()..addPlugin(StandardJsonPlugin())).build(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Built Value": { | |
"prefix": "blt", | |
"body": [ | |
"abstract class ${1} implements Built<${1}, ${1}Builder> {", | |
"\t${0:// fields go here}", | |
"", | |
"\t${1}._();", | |
"", | |
"\tfactory ${1}([updates(${1}Builder b)]) = _$${1};", | |
"}" | |
], | |
"description": "Built Value Class" | |
}, | |
"Enum Class": { | |
"prefix": "encl", | |
"body": [ | |
"class ${1} extends EnumClass {", | |
"\t${0:// fields go here}", | |
"\tstatic const ${1} ${2} = _$${2};", | |
"", | |
"\tconst ${1}._(String name): super(name);", | |
"", | |
"\tstatic BuiltSet<${1}> get values => _$$values;", | |
"\tstatic ${1} valueOf(String name) => _$$value(name);", | |
"}" | |
], | |
"description": "Built Enum Class" | |
}, | |
"Built Value Serializable": { | |
"prefix": "blts", | |
"body": [ | |
"abstract class ${1} implements Built<${1}, ${1}Builder> {", | |
"\t${0:// fields go here}", | |
"", | |
"\t${1}._();", | |
"", | |
"\tfactory ${1}([updates(${1}Builder b)]) = _$${1};", | |
"", | |
"\tString toJson() {", | |
"\t\treturn json.encode(serializers.serializeWith(${1}.serializer, this));", | |
"\t}", | |
"", | |
"\tstatic ${1} fromJson(String jsonString) {", | |
"\t\treturn serializers.deserializeWith(${1}.serializer, json.decode(jsonString));", | |
"\t}", | |
"", | |
"\tString toMap() {", | |
"\t\treturn serializers.serializeWith(${1}.serializer, this);", | |
"\t}", | |
"", | |
"\tstatic ${1} fromMap(Map<String, dynamic> map) {", | |
"\t\treturn serializers.deserializeWith(${1}.serializer, map);", | |
"\t}", | |
"", | |
"\tstatic Serializer<${1}> get serializer => _$${1/(^[A-z]{1})/${1:/downcase}/}Serializer;", | |
"}" | |
], | |
"description": "Serializable Built Value Class" | |
}, | |
"Built Value Header": { | |
"prefix": "blth", | |
"body": [ | |
"library ${1};", | |
"", | |
"import 'dart:convert';", | |
"", | |
"import 'package:built_collection/built_collection.dart';", | |
"import 'package:built_value/built_value.dart';", | |
"import 'package:built_value/serializer.dart';", | |
"", | |
"part '${1}.g.dart';", | |
], | |
"description": "Built Value Imports and File Header" | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment