flutter_stream_redux 0.0.4+1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 10

flutter_stream_redux

Stream helper for build_redux and flutter_build_redux

Getting Started with StreamMiddleware

Add action

In you action file add the action that you want to trigger the stream subscription. The payload of this action should be of type SubscriptionPayload. The type of SubscriptionPayload should be the type of payload you want to receive in the getStream method in the MiddlewareStreamHandler. It can also be of type Null if you don't want to receive anything. In the example we have a payload type of Duration witch is the interval at witch we emit a new value:

ActionDispatcher<SubscriptionPayload<Duration>> get timeStream;

Create the stream middleware

Create middleware

We can create the the middleware using the MiddlewareStreamBuilder and add the actions you've created on the previous step. Note that only actions with the payload type of SubscriptionPayload are accepted.

Middleware<AppState, AppStateBuilder, AppActions> getStreamMiddleware() {
  final MiddlewareStreamBuilder<AppState, AppStateBuilder, AppActions> builder =
      new MiddlewareStreamBuilder<AppState, AppStateBuilder, AppActions>();

  builder..add(AppActionsNames.timeStream, new TimeStreamHandler());

  return builder.build();
}

Add you action handler class

To handle the middleware action you need to create a class that extends MiddlewareStreamHandler<AppState, AppStateBuilder, AppActions, Payload, StreamType>.

The Payload is the type of the SubscriptionPayload<Payload> object you've added in the first step. In getStream you receive the payload you've passed when you called the triggering action.

The StreamType is the type of the object you stream produces, like Stream<StreamType>.

In the getStream method you need to return the stream you want to listen to. onData is called each time a new value was emitted. You can also handle errors and stream close using the other callbacks. You can also chose to cancel the stream on error by overriding the cancelOnError field, default is false.

class TimeStreamHandler extends MiddlewareStreamHandler<AppState, AppStateBuilder, AppActions, Duration, int> {
  static final Log log = new Log('TimeStreamHandler');

  @override
  Stream<int> getStream(Duration payload) {
    //initialize your stream here
  }

  @override
  void onData(MiddlewareApi<AppState, AppStateBuilder, AppActions> api, ActionHandler next,
      Action<SubscriptionPayload<Duration>> action, int event) {
    //handle new data emited
  }

  @override
  void onError(MiddlewareApi<AppState, AppStateBuilder, AppActions> api, dynamic error, StackTrace stackTrace) {
    log.d(error);
  }

  @override
  void onDone(MiddlewareApi<AppState, AppStateBuilder, AppActions> api) {
    log.d('done');
  }
  
  @override
  bool get cancelOnError => false;
}

Add the stream middleware to your store

You just add you stream middleware to you middleware list of your Store.

  final Store<AppState, AppStateBuilder, AppActions> store = new Store<AppState, AppStateBuilder, AppActions>(
    reducerBuilder,
    new AppState.initialState(),
    new AppActions(),
    middleware: <Middleware<AppState, AppStateBuilder, AppActions>>[
      createStoreMiddleware(),
      getStreamMiddleware(),
    ],
  );
      

Start listening to the stream.

To start listening to the stream you can now call the action you've create. When you subscribe you must give the payload you want to receive in getStream method in the MiddlewareStreamHandler. In this way you can manually subscribe and unsubscribe from the stream.

actions.timeStream(SubscriptionPayload.subscribe(duration));

actions.timeStream(SubscriptionPayload.unsubscribe);

Getting Started with StreamedStoreConnector

If you want to remain subscribed to the stream as long as the Widget is on screen then you can use the StreamedStoreConnector<StoreState, Actions, LocalState, Payload> witch automatically manages the subscription for you. This is a thin wrapper around the StoreConnector from the flutter_built_redux package.

There are two more overrides to make this work.

streamAction is the action that triggers the subscription

subscribePayload is the payload you would normally pass to the subscription action. Payload is the type of the SubscriptionPayload

class NowStreamConnector extends StreamedStoreConnector<AppState, AppActions, int, Duration> {
  static final Log log = new Log('NowStreamConnector');

  NowStreamConnector({@required this.builder, @required this.duration});

  final ViewModelBuilder<int> builder;

  final Duration duration;

  @override
  Duration get subscribePayload => duration;

  @override
  ActionDispatcher<SubscriptionPayload<Duration>> streamAction(AppActions actions) => actions.timeStream;

  @override
  int connect(AppState state) => state.now;

  @override
  Widget build(BuildContext context, int now, AppActions actions) {
    return builder(context, now);
  }
}

The logger package is https://pub.dartlang.org/packages/flutter_logger

[0.0.4] - Mar. 1, 2018

  • add type to Action in onData callback
  • add Middleware reference to onError and onDone callbacks
  • update the stream if the action or the subscription payload changes
  • add more content to README
  • add example

[0.0.3] - Feb. 28, 2018

  • BUGFIX: check if the handler is not null before trying to subscribe

[0.0.2] - Feb. 28, 2018

  • moved subscription to didChangeDependencies

[0.0.1] - Feb. 27, 2018

  • initial release.

example/lib/main.dart

import 'package:built_redux/built_redux.dart';
import 'package:example/automatic_stream_connection.dart';
import 'package:example/generated/i18n.dart';
import 'package:example/manual_stream_connection.dart';
import 'package:example/redux/actions.dart';
import 'package:example/redux/middleware.dart';
import 'package:example/redux/models/app_state.dart';
import 'package:example/redux/reducer.dart';
import 'package:example/redux/stream_middleware.dart';
import 'package:flutter/material.dart';
import 'package:flutter_built_redux/flutter_built_redux.dart' hide WidgetBuilder;
import 'package:flutter_localizations/flutter_localizations.dart';

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
  final Store<AppState, AppStateBuilder, AppActions> store = new Store<AppState, AppStateBuilder, AppActions>(
    reducerBuilder,
    new AppState.initialState(),
    new AppActions(),
    middleware: <Middleware<AppState, AppStateBuilder, AppActions>>[
      createStoreMiddleware(),
      getStreamMiddleware(),
    ],
  );

  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return new ReduxProvider(
      store: widget.store,
      child: new MaterialApp(
        title: 'Stream Redux Demo',
        theme: new ThemeData(
          primarySwatch: Colors.blue,
        ),
        supportedLocales: S.delegate.supportedLocales,
        localizationsDelegates: <LocalizationsDelegate<dynamic>>[
          S.delegate,
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
        ],
        home: const ManualStreamConnector(),
        routes: <String, WidgetBuilder>{
          'automatic': (BuildContext context) => const AutomaticStreamConnection(),
        },
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  flutter_stream_redux: ^0.0.4+1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

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

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_stream_redux/flutter_stream_redux.dart';
  
Version Uploaded Documentation Archive
0.0.4+1 Mar 1, 2018 Go to the documentation of flutter_stream_redux 0.0.4+1 Download flutter_stream_redux 0.0.4+1 archive
0.0.4 Mar 1, 2018 Go to the documentation of flutter_stream_redux 0.0.4 Download flutter_stream_redux 0.0.4 archive
0.0.3+1 Feb 28, 2018 Go to the documentation of flutter_stream_redux 0.0.3+1 Download flutter_stream_redux 0.0.3+1 archive
0.0.3 Feb 28, 2018 Go to the documentation of flutter_stream_redux 0.0.3 Download flutter_stream_redux 0.0.3 archive
0.0.2 Feb 28, 2018 Go to the documentation of flutter_stream_redux 0.0.2 Download flutter_stream_redux 0.0.2 archive
0.0.1 Feb 27, 2018 Go to the documentation of flutter_stream_redux 0.0.1 Download flutter_stream_redux 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
20
Health:
Code health derived from static analysis. [more]
0
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
0
Overall:
Weighted score of the above. [more]
10
Learn more about scoring.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Analysis issues and suggestions

Support Dart 2 in pubspec.yaml.

The SDK constraint in pubspec.yaml doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see https://www.dartlang.org/dart-2#migration.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.19.0 <2.0.0