All Projects → ensody → reactive_state

ensody / reactive_state

Licence: MIT license
An easy to understand reactive state management solution for Flutter.

Programming Languages

dart
5743 projects
kotlin
9241 projects
swift
15916 projects
objective c
16641 projects - #2 most used programming language

Projects that are alternatives of or similar to reactive state

getx snippets extension
An extension to accelerate the process of developing applications with flutter, aimed at everyone using the GetX package.
Stars: ✭ 142 (+647.37%)
Mutual labels:  state-management, flutter-plugin, flutter-package
mutable
State containers with dirty checking and more
Stars: ✭ 32 (+68.42%)
Mutual labels:  state-management, state, observable
concave
🧐 Lens-like state management (for React).
Stars: ✭ 13 (-31.58%)
Mutual labels:  state-management, state
swipedetector
A Flutter package to detect up, down, left, right swipes.
Stars: ✭ 34 (+78.95%)
Mutual labels:  flutter-plugin, flutter-package
particule
Fine-grained atomic React state management library
Stars: ✭ 31 (+63.16%)
Mutual labels:  state-management, state
gen lang
gen_lang is a dart library for internationalization. Extracts messages to generate dart files required by Intl, inspired by Intl_translation and Flutter i18n
Stars: ✭ 94 (+394.74%)
Mutual labels:  flutter-plugin, flutter-package
vue
Vue integration for Nano Stores, a tiny state manager with many atomic tree-shakable stores
Stars: ✭ 25 (+31.58%)
Mutual labels:  state-management, state
teaful
🍵 Tiny, easy and powerful React state management
Stars: ✭ 638 (+3257.89%)
Mutual labels:  state-management, state
useSharedState
useSharedState is a simple hook that can be used to share state between multiple React components.
Stars: ✭ 0 (-100%)
Mutual labels:  state-management, state
restate
A redux fractal state library 🕷
Stars: ✭ 55 (+189.47%)
Mutual labels:  state-management, state
riduce
Get rid of your reducer boilerplate! Zero hassle state management that's typed, flexible and scalable.
Stars: ✭ 14 (-26.32%)
Mutual labels:  state-management, state
react-cool-form
😎 📋 React hooks for forms state and validation, less code more performant.
Stars: ✭ 246 (+1194.74%)
Mutual labels:  state-management, state
nepali date picker
Material Style Date Picker with Bikram Sambat(Nepali) Calendar Support. Supports both Android and ios.
Stars: ✭ 30 (+57.89%)
Mutual labels:  flutter-plugin, flutter-package
immerx-state
Reactive, fractal and no-nonsense state management with Immer
Stars: ✭ 19 (+0%)
Mutual labels:  state, observable
agile
🌌 Global State and Logic Library for JavaScript/Typescript applications
Stars: ✭ 90 (+373.68%)
Mutual labels:  state-management, state
survey kit
Flutter library to create beautiful surveys (aligned with ResearchKit on iOS)
Stars: ✭ 68 (+257.89%)
Mutual labels:  flutter-plugin, flutter-package
davinci
A flutter package to convert any widget to an Image.
Stars: ✭ 33 (+73.68%)
Mutual labels:  flutter-plugin, flutter-package
tstate-machine
TypeScript implementation of State Manager(like StateMachine)
Stars: ✭ 20 (+5.26%)
Mutual labels:  state-management, state
jedisdb
redis like key-value state management solution for React
Stars: ✭ 13 (-31.58%)
Mutual labels:  state-management, state
vuex-but-for-react
A state management library for React, heavily inspired by vuex
Stars: ✭ 96 (+405.26%)
Mutual labels:  state-management, state

reactive_state

Pub Build Status

An easy to understand reactive state management solution for Flutter.

Principles

Observable state

State is held in one or multiple instances of Value or similar classes implementing ValueNotifier. These are standard Flutter interfaces that everybody knows from TextEditingController, Animation, etc.

Additionally, you can use ListValue and MapValue for creating observable List and Map values that can notify you about fine-grained change events (instead of the whole value changing).

Reactive widgets

AutoBuild automatically rebuilds your widgets when a ValueNotifier (or any Listenable) triggers a notification. It's similar to Flutter's ValueListenableBuilder, but it can track multiple dependencies and also works with Listenable.

No need to call addListener/removeListener. Just get() the value directly while AutoBuild takes care of tracking your dependencies.

Unlike InheritedWidget and Provider you get fine-grained control over what gets rebuilt.

Standard Flutter classes like TextEditingController and Animation implement ValueListenable and thus work nicely with AutoBuild.

Derived/computed state

DerivedValue is an observable value that is computed (derived) from other observable values.

Also, ListValue and MapValue provide .map() and other operations for creating derived containers that keep themselves updated on a per-element basis.

Less boilerplate and indirection

The resulting code is much simpler than the same solution in BLoC or Redux.

  • No streams, no StreamBuilder, no asynchronous loading of widgets (unless you really need it).
  • No special event objects, no event handlers with long switch() statements.

Usage

Note: Also see reference for details.

A simple AutoBuild example:

import 'package:flutter/material.dart';
import 'package:reactive_state/reactive_state.dart';

class MyPage extends StatelessWidget {
  MyPage({Key key, @required this.counter}) : super(key: key);

  final ValueNotifier<int> counter;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Counter')),
      body: Column(
        children: <Widget>[
          AutoBuild(builder: (context, get, track) {
            return Text('Counter: ${get(counter)}');
          }),
          MaterialButton(
            onPressed: () => counter.value++,
            child: Text('Increment'),
          ),
        ],
      ),
    );
  }
}

Note that in real-world applications you shouldn't directly mutate the state, but instead put that into separate methods e.g. on an object made accessible through the provider package.

Also, take a look at the example in the repo.

autoRun and AutoRunner

Outside of widgets you might still want to react to state changes. You can do that with autoRun() and AutoRunner (see reference for details).

Value vs ValueNotifier

As an alternative to ValueNotifier you can also use reactive_state's Value class which provides an update() method for modifying more complex objects:

class User {
  String name = '';
  String email = '';
  // ...
}

var userValue = Value(User());
userValue.update((user) {
  user.name = 'Adam';
  user.email = '[email protected]';
});

This is similar to calling setState() with StatefulWidget. With update() you can change multiple attributes and Value will trigger a single notification once finished - even if nothing was changed (so you don't need to implement comparison operators for complex objects).

DerivedValue

DerivedValue is a dynamically calculated ValueListenable that updates its value whenever its dependencies change:

var user = Value(User());
var emailLink = DerivedValue((get, track) => 'mailto:${get(user).email}');

Here, emailLink can be observed on its own and is updated whenever user is modified.

ListValue and MapValue

A simple example showing a few things that can be done:

final listValue = ListValue(<int>[]);
final mappedList = listValue.map((x) => x.toString());
final listToMap = mappedList.toMap((x) => MapEntry(2 * int.parse(x), x));
final invertedMap = listToMap.map((k, v) => MapEntry(v, k));

listValue.addAll([4, 1]);
// => invertedMap.value == {'4': 8, '1': 2}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].