streams_provider 0.0.4+7

  • Readme
  • Changelog
  • Example
  • Installing
  • 70

Streams Provider #

Coverage GitHub issues GitHub stars GitHub license


It provides an elegant and effectively ways to use stream in Provider.

Streams Widgets #

StreamsProvider is a Flutter widget which provides a provider to its children via Provider.of<T>(context). It is used as a dependency injection (DI) widget so that a single instance of a provider can be provided to multiple widgets within a subtree.

In most cases, StreamsProvider should be used to create new provider which will be made available to the rest of the subtree. In this case, since StreamsProvider is responsible for creating the provider, it will automatically handle closing the provider.

StreamsProvider(
  create: (BuildContext context) => ProviderA(),
  child: ChildA(),
);

In some cases, StreamsProvider can be used to provide an existing provider to a new portion of the widget tree. This will be most commonly used when an existing provider needs to be made available to a new route. In this case, StreamsProvider will not automatically close the bloc since it did not create it.

StreamsProvider.value(
  value: StreamsProvider.of<ProviderA>(context),
  child: ScreenA(),
);

then from either ChildA, or ScreenA we can retrieve ProviderA with:

// with extensions context.provider<ProviderA>();

// without extensions StreamsProvider.of<ProviderA>(context)

StreamsSelector is a Flutter widget which select streams from a provider and invokes the builder in response to signal emits in the selector stream.

StreamsSelector<ProviderA, DataType>(
  selector: (context, provider) {
     // select streams from ProviderA
  },
  builder: (context, data, child) {
    // return widget here based on selector stream emits
  },
)

StreamsListener is a Flutter widget which takes a StreamsWidgetListener and a selector and invokes the listener in response to signal emits in the selector. It should be used for functionality that needs to occur once per signal emit such as navigation, showing a SnackBar, showing a Dialog, etc... listener is only called once for each signal emit.

StreamsListener<ProviderA, DataType>(
  selector: (context, provider) {
     // select streams from ProviderA
  },
  listener: (context, data) {
    // do stuff here based on data changes
  },
  child: Container(),
)

Usage #

Lets take a look at how to use StreamsSelector to hook up a CounterPage widget to a CounterProvider.

counter_provider.dart #

class CounterProvider implements StreamsProvidable {
  final _counter = MutableValueStream<int>(0);
  ValueStream<int> counter => _counter;

  void incrementCounter() {
    _counter.value++;
  }

  void decrementCounter() {
    _counter.value—;
  }
}

counter_page.dart #

class CounterPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final CounterProvider counterProvider = context.provider<CounterProvider>();

    return Scaffold(
      appBar: AppBar(title: Text('Counter')),
      body: StreamsSelector<CounterProvider, int>(
        selector: (context, provider) => provider.counter,
        builder: (context, count, child) {
          return Center(
            child: Text(
              '$count',
              style: TextStyle(fontSize: 24.0),
            ),
          );
        },
      ),
      floatingActionButton: Column(
        crossAxisAlignment: CrossAxisAlignment.end,
        mainAxisAlignment: MainAxisAlignment.end,
        children: <Widget>[
          Padding(
            padding: EdgeInsets.symmetric(vertical: 5.0),
            child: FloatingActionButton(
              child: Icon(Icons.add),
              onPressed: () {
                counterProvider.incrementCounter();
              },
            ),
          ),
          Padding(
            padding: EdgeInsets.symmetric(vertical: 5.0),
            child: FloatingActionButton(
              child: Icon(Icons.remove),
              onPressed: () {
                counterProvider.decrementCounter();
              },
            ),
          ),
        ],
      ),
    );
  }
}

0.0.4+7 #

  • Add debug function on Stream
  • Change StreamsDisposeBag as a Future

0.0.4+6 #

  • Fix bugs on StreamsSelector: need to compare data

0.0.4+5 #

  • Fix bugs on StreamsSelector

0.0.4+4 #

  • Fix behaviour with null value on StreamsSelector

0.0.4+3 #

  • Throw error when stream got an error

0.0.4+2 #

  • Update home_url.

0.0.4 #

  • Extension TextEditingController with textStream(), selectionStream(), valueStream().

0.0.3 #

  • MutableValueStream no longer has an empty stream. Initial event is null if doesn't define.
  • Add StreamsListenable class.
  • Add StreamsDisposeBag class.

0.0.2 #

  • Change package name to streams_provider.
  • Add StreamsListener widget.

0.0.1 #

  • Initial release.

example/lib/main.dart

import 'package:example/sign_in_provider.dart';
import 'package:example/sign_in_screen.dart';
import 'package:flutter/material.dart';
import 'package:streams_provider/streams_provider.dart';

void main() {

  runApp(App());
}

class App extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: StreamsProvider<SignInProvider>(
        create: (_) => SignInProvider(),
        child: SignInScreen(),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  streams_provider: ^0.0.4+7

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:streams_provider/streams_provider.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
43
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
70
Learn more about scoring.

We analyzed this package on Mar 31, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Health suggestions

Format lib/src/matchers.dart.

Run flutter format to format lib/src/matchers.dart.

Format lib/src/mutable_value_stream.dart.

Run flutter format to format lib/src/mutable_value_stream.dart.

Format lib/src/streams_dispose_bag.dart.

Run flutter format to format lib/src/streams_dispose_bag.dart.

Format lib/src/streams_selector.dart.

Run flutter format to format lib/src/streams_selector.dart.

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.6.0 <3.0.0
collection ^1.14.11 1.14.11 1.14.12
flutter 0.0.0
provider ^4.0.4 4.0.4 4.1.0-dev+2
rxdart ^0.23.1 0.23.1 0.24.0-dev.1
Transitive dependencies
meta 1.1.8
nested 0.0.4
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_coverage_badge ^0.0.1+1
flutter_test
mockito ^4.1.1
test any