obsobject 1.0.3

  • Readme
  • Changelog
  • Example
  • Installing
  • new63

Obsobject #

An observable, computed objects written in Dart with built-in validation

Install #

dependencies:  
    obsobject: ^1.0.0

Observable #

An object will notify to all listeners/observers after its value changed.

Example:

var a = Observable('test');
a.listen(() {
    print(a.value);
});
a.value = 'First test';

Output result:
test
First test

Computed #

It's an observable too, but its value will be calculated from other observable object.
It's smart to know which observable object that it's depend on, and auto rebuild value when value of dependencies has changed.

Example:

var a = Observable(false);
var b = Observable(1);
var c = Computed(() => a.value ? b.value*10 : 0);

print(c.value); //result 0, depend on only 'a';
b.value = 2;
print(c.value); //result 0, depend on only 'a'
print(c.rebuildCount); //result 1, it is not recalculate;
a.value=true;
print(c.value);//result 20, depend on 'a', 'b'

Computed object is only recalculate if there are listeners on it or when access to its value.
And the calculation is a async process, so observable can change value many times, but Computed just run one

Example:

var a = Observable(0);
var b = Computed(() => a.value);
b.changed(()=>print(b.value));
for(var i=0; i<1000; i++) a.value=i;
print(b.rebuildCount); //result 1

Computed is just give readonly value, thus there are a object Commission used to read+write value

Validator #

Built-in validate:

  • required: value can not null, empty string, zero
  • email: value must be valid email address
  • range: check number value, string length, array length
  • pattern: check string matched RegExp
  • contains: value must in array
  • unique: value not exist in array
  • true: value must be true
  • custom: check value by a custom function
  • async: check value by a async function
  • all: combine and check all validators
  • least: combine and check all validators but it stopped at the first invalid
  • not: negative a validator

Observable has a property isValid, it's an observable value of validation status, and it also has features:

  • condition: used to check something before execute validate
  • message: custom message, default message, support for localize
  • And easy way to custom or extends new validation

Example:

//use with Validator....
var email = Observable('')
            ..isValid.validator = 
                ValidatorLeast([ValidatorRequired(), ValidatorEmail()]);

//use with Map data config
var email = Observable('')
            ..isValid.validator = 
                Validator.convert({
'least': {
    'validators': {'required': 'Email is required', 'email': true}
}
                });
//current status
print(email.isValid.value);
//current invalid message
print(email.isValid.message);
//listen on validation
email.isValid.listen(() {
//do something
});

View more details in Wiki, API #

CHANGE LOG #

1.0.3 #

Support listener function with parameters
Reuse CancelableThen in ObservableValidator
Spell check

1.0.2 #

Update follow dart analyze Add example widget in Flutter

1.0.1 #

Update follow publish suggestions

1.0.0 #

First release

example/example.dart

import 'package:obsobject/obsobject.dart';

class User {
  final Observable<String> firstName = Observable('')
    ..isValid.validator = ValidatorRequired();
  final Observable<String> lastName = Observable('')
    ..isValid.validator = ValidatorRequired();
  final Observable<String> email = Observable('')
    ..isValid.validator = Validator.convert({
      'least': {'required': 'Email is required', 'email': true}
    });
  Computed<String> fullName;
  User() {
    fullName = Computed(() => '${firstName.value}  ${lastName.value}');
  }
  bool get isValid {
    return firstName.isValid.value &&
        lastName.isValid.value &&
        email.isValid.value;
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  obsobject: ^1.0.3

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:obsobject/obsobject.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
27
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
63
Learn more about scoring.

We analyzed this package on Jul 6, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
Dev dependencies
test ^1.14.7