GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - alselawi/streamify: Simple app state in flutter

BranchesTags
Open more actions menu

Latest commit

 

History

4 Commits
4 Commits

Folders and files

NameName
Last commit message
Last commit date
lib
lib
 
 
test
test
 
 
.gitignore
.gitignore
 
 
CHANGELOG.md
CHANGELOG.md
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
analysis_options.yaml
analysis_options.yaml
 
 
pubspec.lock
pubspec.lock
 
 
pubspec.yaml
pubspec.yaml
 
 

Repository files navigation

Streamify

yet-another-state-management-library for flutter, but:

  • Zero boilerplate.
  • Simple and easy to use.
  • No Jargon.
  • No magic.

Simply put: this library would take a value and make into a stream, thus makes it consumable by StreamBuilder.

import 'package:streamify/streamify.dart';

/// creates a stream:
final counter = Streamify(0);

/// get the current value:
counter();

/// set a new value:
counter(1);

That's it!

Flutter Widget Consumption

You would consume the stream in flutter using the StreamBuilder widget:

import 'package:flutter/material.dart';

final counter = Streamify(0);

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
        stream: counter.stream,
        builder: (context, snapshot) {
          return Text(counter().toString()); // accessing the current value
        },
    );
  }
}

/// now whenever you call counter(1) the text will change to 1

That's all you need to know to use this library. However, there's few more things that you can do:

  1. The library gives you a utility widget that would watch multiple streams (instead of one) and update the UI accordingly: MStreamBuilder:

    import 'package:flutter/material.dart';
    
    final a = Streamify("a");
    final b = Streamify("b");
    
    class MyWidget extends StatelessWidget {
        @override
        Widget build(BuildContext context) {
            return MStreamBuilder(
                streams: [a.stream, b.stream],
                builder: (context, snapshot) {
                return Text("${a()} ${b()}");
                },
            );
        }
    }
  2. You can change the value without emitting a new event (i.e without notifying the observers or updating the UI)

    final counter = Streamify(0);
    counter(1); // this will update the UI
    counter.silently(() => counter(10)); // silently set the value to 10
  3. You can call .observe or .unObserve to add/remove custom observers to the stream.

  4. You can call .dispose to dispose the stream and remove all observers, and prevent any further observations.

But how to structure my state?

Given that this library is very simple, and the state is just a final variable, you can structure your state however you want, here's an example that creates a state class for a counter app:

import 'package:streamify/streamify.dart';

class _CounterAppState {
  // state
  final counter = Streamify(0);
  final isLoading = Streamify(false);

  // computed properties
  get counterByTwo => counter() * 2;

  // actions (methods)
  void reset() {
    counter(0);
  }

  void increment() {
    counter(counter() + 1);
  }

  void decrement() {
    counter(counter() - 1);
  }

  void fetchData() async {
    isLoading(true);
    await Future.delayed(Duration(seconds: 1));
    counter(counter() + 1);
    isLoading(false);
  }
}


// create an instance of the state
final counterAppState = _CounterAppState();

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT License

About

Simple app state in flutter

Resources

Readme
MIT license
Activity

Stars

0 stars

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.