Last updated:
0 purchases
published
Motivation #
Published was inspired by Published property wrapper from Swift.
There are approaches like using BloC that depend on streams and they introduce a lot of boilerplate.
Published goal is reduce that boilerplate and introduce simple state management.
How to use #
Installation #
To use package:published in your package, add these
dependencies to your pubspec.yaml.
dependencies:
published:
dev_dependencies:
build_runner:
published_gen:
copied to clipboard
Usage #
import 'package:published/published.dart';
part 'counter_view_model.published.dart';
@published
abstract class CounterViewModel extends _$CounterViewModel {
@Publisher()
abstract int count;
void increment() => count++;
@override
bool get enableLogging => true;
}
copied to clipboard
You'll have to make abstract class and annotate it with @published and extend it with _$ClassName.
Annotate abstract field for which you want to expose stream with @Publisher().
Note: It's important to use abstract fields and classes so we can replicate Swift structs.
Then run pub run build_runner build to generate files into your source directory.
> pub run build_runner build
[INFO] ensureBuildScript: Generating build script completed, took 368ms
[INFO] BuildDefinition: Reading cached asset graph completed, took 54ms
[INFO] BuildDefinition: Checking for updates since last build completed, took 663ms
[INFO] Build: Running build completed, took 10ms
[INFO] Build: Caching finalized dependency graph completed, took 44ms
[INFO] Build: Succeeded after 4687ms with 1 outputs
copied to clipboard
NOTE: If you're using Flutter, replace pub run with
flutter packages pub run.
To use it do following
//Init
final model = CounterViewModelBuilder.build(count: 0);
...
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
StreamBuilder<int>(
stream: model.$count,
builder: (context,snapshot) => Text("${snapshot.data ?? 0}");
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () => model.count++,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
copied to clipboard
$classNameBuilder.build() needs to be used to create instance of the object. If you don't want to pass default value in build() use defaultValue property on Publisher like this:
@Publisher(defaultValue: "John")
abstract String name;
copied to clipboard
To observe if any property annotated with Publisher changed you can use didChange:
model.didChange.listen(() => doSomething())
copied to clipboard
If you want to enableLogging you can override getter enableLogging:
@published
abstract class CounterViewModel extends _$CounterViewModel {
...
@override
bool get enableLogging => true;
}
copied to clipboard
Finally if you want to do something on object creation you can override onBind method and place your logic there:
@published
abstract class CounterViewModel extends _$CounterViewModel {
...
@override
@override
void onBind() {
super.onBind();
fetchSomeData();
}
}
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.