GithubHelp home page GithubHelp logo

chaaarles / flutter_observable_state Goto Github PK

View Code? Open in Web Editor NEW

This project forked from christianalfoni/flutter_observable_state

0.0 0.0 0.0 13 KB

Observable state for flutter applications

License: MIT License

Dart 100.00%

flutter_observable_state's Introduction

flutter_observable_state

Observable state for flutter applications

Motivation

Coming from the world of state management in Javascript/Typescript, I felt that the current solutions to state management with Flutter was too verbose. Building libraries like CerebralJS and OvermindJS, I took inspiration from that work and built this simple approach to managing state in Flutter.

The concept

class AppState {
  final count = Observable(0);
}

Now you have an observable piece of state, lets use it in a widget.

// For simplicity we just instantiate, please read
// further for proper setup
final _state = AppState()

class MyWidget extends StatelessWidget {
  @override
  Widget build(context) {
    return Container(
      child: observe(() => (
        Row(
          children: [
            Text(_state.count.get()),
            FlatButton(
              onPressed: () {
                _state.count.change((count) => count + 1)
              },
              child: Text("Increase count")
            )
          ]
        )
      ))
    )
  }
}

Any widgets returned within the scope of the observe function will rerender when any state it accesses changes. You can use as many observe you want within a widget, even nest them.

Organizing your project

When you think about state as application state you will rather define and change the state of the application outside of your widgets. You will still need local widget state, but you primarily want to put your state outside the widgets. To effectively share this state and the logic to change it with all widgets of your application it is highly recommended to use the get_it project. Let us create our initial setup.

We want to create two classes. AppState and Actions. We can use get_it to create a single instance of these classes, which can then be used in any widget.

// services.dart
import 'package:my_project/AppState.dart';
import 'package:my_project/Actions.dart';
import 'package:get_it/get_it.dart';

final getIt = GetIt();

void initialize() {
  getIt.registerSingleton(AppState());
  getIt.registerSingleton(Actions());
}

You can use this file to also register effects you want to perform. For example classes that manages communication with Firebase etc.

// AppState.dart
import 'package:flutter_observable_state/flutter_observable_state.dart';

class AppState {
  final count = Observable(0);
}

// Actions.dart
import 'package:my_project/AppState.dart';
import 'package:my_project/services.dart';

class Actions {
  final _state = getIt.get<AppState>();

  void changeCount(int count) {
    _state.count.change((currentCount) => currentCount + count)
  }
}

Now in a widget you are able to do:

class MyWidget extends StatelessWidget {
  final _state = getIt.get<AppState>()
  final _actions = getIt.get<Actions>()

  @override
  Widget build(context) {
    return Container(
      child: observe(() => (
        Row(
          children: [
            Text(_state.count.get()),
            FlatButton(
              onPressed: () {
                _actions.changeCount(1)
              },
              child: Text("Increase count")
            )
          ]
        )
      ))
    )
  }
}

We have now effectively allowed any widget to access our count and any widget can change it, making sure that any widget observing the state will rerender.

API

Observable

Create an observable value.

Observable(0);
Observable<List<String>> = Observable([]);
Observable<User>(null);

Observable.get

Get the value of an Observable.

var count = Observable(0);

count.get(); // 0

Observable.set

Set the value of an Observable.

var count = Observable(0);

count.set(1);

Observable.change

Change the value of an Observable.

var count = Observable(0);

count.change((currentCount) => currentCount + 1);

Observable.setStream

Connect a stream of values, making the Observable update whenever the stream passes a new value.

var user = Observable<FirebaseUser>(null);

user.setStream(FirebaseAuth.instance.onAuthStateChanged);

// Unset stream
user.setStream(null);

When a stream is set you can still set and change to a new value.

Computed

You can derive state. This works much like the observe, but it only flags the computed as dirty. The next time something gets the value, it will be recalculated.

var foo = Observable('bar');
var upperFoo = Computed(() => foo.get().toUpperCase());

You will typically define computeds with your AppState class.

class AppState {
  final foo = Observable('bar');

  Computed<String> upperFoo;

  AppState() {
    upperFoo = Computed(() => foo.get().toUpperCase());
  }
}

observe

To observe state in widgets you use the observe function. It returns a StreamBuilder and can be used wherever you typically insert a child widget.

class MyWidget extends StatelessWidget {
  final _state = getIt.get<AppState>();

  @override
  Widget build(context) {
    return Container(
      child: observe(() => (
        Text(_state.foo.get())
      ))    
    )
  }
}

Reaction

You can observe state and react to it. This is useful when you need to do some imperative logic inside your widgets. For example here we are controlling an overlay from our application state:

class MyWidget extends StatefulWidget {
  @override
  createState() => MyWidgetState();
}

class MyWidgetState extends State<MyWidget> {
  final _state = getIt.get<AppState>();
  Reaction reaction;
  OverlayEntry overlay;

  @override
  void initState() {
    reaction = Reaction(
      () => _state.isOverlayOpen.get(),
      () => {
        if (_state.isOverlayOpen.get() && overlay == null) {
          overlay = _createOverlayEntry();
          Overlay.of(context).insert(overlay);
        } else if (!_state.isOverlayOpen.get() && overlay != null) {
          overlay.remove();
          overlay = null;
        }
      }
    )
    super.initState();
  }

  @override
  Widget build(context) {
    return Container(
      child: observe(() => (
        Text(_state.foo.get())
      ))
    )
  }
}

Models

You can use Observable with whatever classes you want, even inside widgets. Typically though you want to use it with classes representing models. For example you want to track optimistically adding a like to posts.

// Post.dart
class Post {
  String id;
  String title;
  String description;
  Observable<bool> likesCount;

  Post.fromJSON(Map<String, dynamic> json) :
    id = json["id"],
    title = json["title"],
    description = json["description"],
    likesCount = Observable(json["likesCount"]);
}

// AppState.dart
class AppState {
  final posts = Observable<List<Post>>([]);
}

// Actions.dart
class Actions {
  final _state = getIt.get<AppState>();
  final _api = getIt.get<Api>();

  void initialize() {
    _state.posts.setStream(_api.$posts);
  }

  void likePost(Post post) {
    post.likesCount.change((likesCount) => likesCount + 1);
    _api.likePost(post.id);
  }
}

How does it work?

Dart is a single threaded language, meaning that only one observe can run at any time. That means the library orchestrates the execution of observe and Computed with the execution of any Observable.get globally. The Computed are considered to live as long as the application lives, while observe uses the StreamBuilder where it clears out existing subscriptions when it builds and when the widget is disposed.

flutter_observable_state's People

Contributors

christianalfoni avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.