simple_property_observer_builder

Creator: coderz1093

Last updated:

0 purchases

simple_property_observer_builder Image
simple_property_observer_builder Images

Languages

Categories

Add to Cart

Description:

simple property observer builder

simple_property_observer_builder #
Dart package for auto generating a property observable class.
This package is only for dev_environment.
See also simple_property_observer package.
How to use #
This repository consists of 2 packages, simple_property_observer and simple_property_observer_builder.
These packages can be used in pure Dart projects as well as Flutter.
example is a simple example of using these packages.
Dependency settings #
In your pubspec.yaml, write the below settings:
dependencies:
simple_property_observer: any

dev_dependencies:
build_runner: any
simple_property_observer_builder: any
copied to clipboard
simple_property_observer contains the definitions for the property observable pattern and the auto-generating annotations.
simple_property_observer_builder contains the command line tools for the auto-generating.
Define your class #
For example, write the below annotated class:
import 'package:simple_property_observer/simple_property_observer.dart';

part 'example.g.dart';

@propertyObservable
class Example {
@observable
String? get observableProperty => null;
}
copied to clipboard
Dart Analysis will point out the error, but ignore it and proceed to the next step.
Points

Annotate your class with @propertyObservable for auto-generating the property observable pattern.
Create GETTER and annotate it with @observable.
Write part sentence for the auto-generating file.

Then, run the build command in Terminal:
$ flutter packages pub run build_runner build
copied to clipboard
If you create a pure Dart project, run the below command:
$ dart run build_runner build
copied to clipboard
After finishing the command, you can see example.g.dart file:
// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'example.dart';

// **************************************************************************
// PropertyObservableGenerator
// **************************************************************************

mixin _ObservableMembers on PropertyObservable {
String? __observableProperty;
set observableProperty(String? value) {
if (__observableProperty == value) {
return;
}
final oldValue = __observableProperty;
__observableProperty = value;
notifyPropertyChanged(PropertyChangedInfo(
'observableProperty', oldValue, __observableProperty));
}
}
copied to clipboard
Point
Then, you must modify example.dart as below:
import 'package:simple_property_observer/simple_property_observer.dart';

part 'example.g.dart';

@propertyObservable
class Example with PropertyObservable, _ObservableMembers {
@observable
String? get observableProperty => __observableProperty;
}
copied to clipboard
Add with PropertyObservable, _ObservableMembers and modify the getter from null to __observableProperty.
Observe instances #
You can observe the property changes with the registerObserver method:
import 'example.dart';

void main() {
final example = Example();

// Observe property changes
example.registerObserver((info) {
print('${info.name} changed: from ${info.oldValue} to ${info.newValue}');
});

print('set Apple');
example.observableProperty = 'Apple';

print('set Banana');
example.observableProperty = 'Banana';

// Shows the output below:
// set Apple
// observableProperty changed: from null to Apple
// set Banana
// observableProperty changed: from Apple to Banana
}
copied to clipboard
You can also cancel observing with the unregisterObserver method.

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.