google / google/built_value.dart

Allow inherrited BuiltValueHooks

Open
#1,310 1 comment 1 reaction 1 assignee Claimed by @davidmorgan View on GitHub
p2 / feature request
Dominant language
Dart
Stars
886
Forks
195
Avg merge
1d 11h
Merged PRs (30d)
4

Description

When making use of interfaces one might wish to add hooks to it that should be invoked by the subclasses.
An example is when setting default (initializeHook) and validating the builder (finalizeHook).
Currently one has to manually add the builders to the subclasses.
Is this a feature you are interested in?

```dart
@BuiltValue(instantiable: false)
abstract interface class TestInterface {
String get value;
@BuiltValueHook(initializeBuilder: true)
static void _defaults(TestInterfaceBuilder b) {
b.value = '123';
}

@BuiltValueHook(finalizeBuilder: true)
static void _validate(TestInterfaceBuilder b) {
if (b.value!.isEmpty) {
throw Error();
}
}
}

abstract class Test implements TestInterface, Built {
factory Test([void Function(TestBuilder)? b]) = _$SuperObject;
const Test._();

static Serializer get serializer => _$superObjectSerializer;

@BuiltValueHook(initializeBuilder: true)
static void _defaults(TestBuilder b) {
TestInterface._defaults(b);
}

@BuiltValueHook(finalizeBuilder: true)
static void _validate(TestBuilder b) {
TestInterface._validate(b);
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.