stream_variable

A simple statemanagement package

Usage

To use this plugin, add stream_variable as a dependency in your pubspec.yaml file.

Installation

stream_variable: 

Example

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

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  //* stream variable type counter
  late StreamVariable<int> _counter;

  @override
  void initState() {
    //* initialize StreamVarible instance
    _counter = StreamVariable<int>();

    //* now initialize value in it
    _counter.setVariable = 0;

    //* now add to variableSink
    _counter.variableSink.add(_counter.getVariable);

    super.initState();
  }

  @override
  void dispose() {
    //* disposing the stream
    _counter.disposeStream();

    super.dispose();
  }

  //* method to increment counter
  void _incrementCounter() {
    setState(() {
      //* first get variable value then update it by setting it
      _counter.setVariable = _counter.getVariable + 1;

      //* then add to variable sink
      _counter.variableSink.add(_counter.getVariable);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            StreamBuilder<int>(

                //* use variable stream to stream varaible
                initialData: 0,
                stream: _counter.variableStream,
                builder: (context, AsyncSnapshot<int> snapshot) {
                  return Text(
                    '${snapshot.data}',
                    style: Theme.of(context).textTheme.headline4,
                  );
                }),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

Class methods and variables

variableSink : Getter for StreamSink<T>
variableStream : Getter for Stream<T>
getVariable : Getter for variable
setVariable : Setter for variable
disposeStream : method to dispose or close the stream

https://github.com/ANGADJIT/stream_variable

Libraries

stream_variable