What is the setter of a built_value’s object. BuiltValue classes are immutable. That’s one of its main features.
Immutability means you can’t modify an instance. Each modification has to result in a new instance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | import 'package:built_value/standard_json_plugin.dart'; import 'package:example/generics.dart'; import 'package:example/polymorphism.dart'; import 'package:example/serializers.dart'; import 'package:example/values.dart'; /// Simple usage examples for built_value. void example() { // Values must be created with all required fields. final value = SimpleValue((b) => b..anInt = 3); // Nullable fields will default to null if not set. final value2 = SimpleValue((b) => b ..anInt = 3 ..aString = 'three'); // All values implement operator==, hashCode and toString. assert(value != value2); // Values based on existing values are created via "rebuild". final value3 = value.rebuild((b) => b..aString = 'three'); assert(value2 == value3); // Or, you can convert to a builder and hold the builder for a while. final builder = value3.toBuilder(); builder.anInt = 4; final value4 = builder.build(); assert(value3 != value4); // Nested built_value fields are built with nested builders. final value5 = CompoundValue((b) => b ..simpleValue.anInt = 1 ..validatedValue.anInt = 2); // Values can use generics. final value6 = GenericValue<String>((b) => b..value = 'string'); // Values with a simplified factory still have a builder. final value7 = VerySimpleValue(3); final value8 = value7.rebuild((b) => b..value = 4); // Values can use polymorphism. final animals = <Animal>[ Cat((b) => b ..legs = 3 ..tail = true), Fish((b) => b ..legs = 0 ..fins = 4), ]; final modifiedAnimals = animals.map((animal) => animal.rebuild((b) => b.legs++)).toList(); // Everything is serializable. for (var object in [ value, value2, value3, value4, value5, value6, value7, value8, modifiedAnimals[0], modifiedAnimals[1], ]) { var serialized = serializers.serialize(object); print(serialized); assert(serializers.deserialize(serialized) == object); } // See chat_example and end_to_end_test for more complex usage! } /// Examples of using StandardJsonPlugin. /// /// The plugin changes the built_value serialization format to the Map-based /// format used by most JSON APIs. /// /// Where needed the plugin specifies which type to serialize/deserialize using /// a `discriminator` field. By default this field is called `$`; you can pass /// a different name to the `StandardJsonPlugin` constructor. If the wire /// format should not contain the type name, you must instead explicitly /// specify which type to serialize/deserialize. Both cases are shown below. void standardJsonExample() { final standardSerializers = (serializers.toBuilder()..addPlugin(StandardJsonPlugin())).build(); // In this first example we know the type we want to serialize/deserialize, // so the type is not mentioned on the wire. final serializedAccount = { 'id': 3, 'name': 'John Smith', 'keyValues': { 'visited': 1732, 'active': true, 'tags': [74, 123, 4001], 'preferences': { 'showMenu': true, 'skipIntro': true, 'colorScheme': 'light', } } }; // Use the deserializeWith method to specify what type you're deserializing. final value = standardSerializers.deserializeWith( Account.serializer, serializedAccount); print(value); assert(value.id == 3); assert(value.name == 'John Smith'); assert(value.keyValues['tags'].asList[2] == 4001); assert(value.keyValues['preferences'].asMap['colorScheme'] == 'light'); // Use the serializeWith method to specify what type you're serializing. final serializedAgain = standardSerializers.serializeWith(Account.serializer, value); assert(serializedAccount.toString() == serializedAgain.toString()); // In this second example we don't know the type we want to // serialize/deserialize, so it has to be specified on the wire. final serializedAccountWithDiscriminator = { r'$': 'Account', 'id': 3, 'name': 'John Smith', 'keyValues': { 'visited': 1732, 'active': true, 'tags': [74, 123, 4001], 'preferences': { 'showMenu': true, 'skipIntro': true, 'colorScheme': 'light', } } }; // We don't have to specify the type when deserializing. final value2 = standardSerializers.deserialize(serializedAccountWithDiscriminator); print(value2); assert(value == value2); // We don't have to specify the type when serializing. final serializedAgain2 = standardSerializers.serialize(value2); assert(serializedAccountWithDiscriminator.toString() == serializedAgain2.toString()); } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.