Git Product home page Git Product logo

erf / mu_state Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 267 KB

A set of helpers for pragmatic state handling as mentioned in my Medium article

Home Page: https://pub.dev/packages/mu_state

License: MIT License

Kotlin 0.28% Swift 2.65% Objective-C 0.09% Dart 29.00% HTML 8.99% CMake 18.82% C++ 38.49% C 1.68%
state-management flutter state flutter-state-management dart valuenotifier valuelistenablebuilder

mu_state's Introduction

mu_state

A minimal state solution based on my Pragmatic state handling in Flutter Medium article .

Features

A set of classes built on ValueNotifier and ValueListenableBuilder for handling state in Flutter.

  • MuState - a ValueNotifier of type MuEvent<T>
  • MuBuilder - a ValueListenableBuilder of type MuEvent<T>
  • MuEvent<T> - the base class for our 3 state object
    • MuEventData<T> - the data state of type T
    • MuEventError - the error state
    • MuEventLoading - the loading state
  • MuMultiBuilder - listen to changes in a list of MuState objects

How To

Declare state as a MuState<T> variable and pass it an initial MuEvent type, e.g. a MuEventLoading or a MuEventData<T>.

For more complex state objects, extend MuState and implement the necessary methods.

Listen to MuState changes using MuBuilder.

Listen to multiple MuState objects using MuMultiBuilder.

Example

In test_state.dart:

import 'package:mu_state/mu_state.dart';

class CounterState extends MuState<int> {
  CounterState(super.initValue);

  void increment() {
    value = MuEventData((value as MuEventData<int>).data + 1);
  }
}

final counterState = CounterState(const MuEventData(0));

In main.dart:

Scaffold(
  body: Center(
    child: MuBuilder(
      state: counterState,
      builder: (context, event, child) {
        return switch (event) {
          MuEventLoading() => const CircularProgressIndicator(),
          MuEventError(error: Object error) => Text('Error: $error'),
          MuEventData(data: int value) => Text('$value')
        };
      },
    ),
  ),
),

Also see Example.

mu_state's People

Contributors

erf avatar

Stargazers

 avatar

Watchers

 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.