Built Value Tutorial for Dart & Flutter

5  comments

 

Data classes, immutability, simple serialization – this is what Dart does NOT offer right off the bat. While Dart is surely one of the better languages with regards to its simplicity, it currently lacks the support for value types.

When you compare two objects of the same type with identical contents, they will still turn out to be not equal – by default, Dart checks for referential equality, not value equality. Making a copy of an object is also painful – you have so much code to write… And serialization? Oh, even more repetitive code for you!

Well, not with built_value library. It will save you so much time by allowing you to write good quality code without all the repetition.

?Get the code from this tutorial?

 

Dart built_value snippets

{
    "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"
    },
    "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}",
            "",
            "\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"
    },
}
Icons made by Becris from www.flaticon.com is licensed by CC 3.0 BY

About the author 

Matt Rešetár

Matt is an app developer with a knack for teaching others. Working as a freelancer and most importantly developer educator, he is set on helping other people succeed in their Flutter app development career.

You may also like

  • Hi Matej, congratulations! Excellent material about ‘built_value’!

    I have a question.

    How could i implement a named constructor like this: “MyClass.fromMap(String data) {…}’

    Look at the example below, i have a custom initialization to the field ‘userRole’:

    Home.fromMap(Map data) {
    this.companyCode = data[‘companyCode’];
    this.companyName = data[‘companyName’];
    this.companyLogo = data[‘companyLogo’];
    this.userRole = UserRole.valueOf(data[‘userEmail’]); // data[‘userEmail’] returns the userRole string
    }

    TIA

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
    >