flutter_stream_redux 0.0.4+1

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

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

Analysis

We analyzed this package on May 22, 2018, and provided a score, details, and suggestions below. Analysis was completed with status tool failures using:

  • Dart: 2.0.0-dev.54.0
  • pana: 0.11.1
  • Flutter: 0.4.4

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
13 / 100
Health:
Code health derived from static analysis. [more]
99 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80 / 100
Overall score:
Weighted score of the above. [more]
52
Learn more about scoring.

Platforms

Detected platforms: unsure

Error(s) prevent platform classification:

Fix dependencies in pubspec.yaml.

Suggestions

  • Fix dependencies in pubspec.yaml.

    Running flutter packages pub upgrade failed with the following output:

    ERR: The current Flutter SDK version is 0.4.4.
     
     Because flutter_stream_redux requires Flutter SDK version ^0.1.2, version solving failed.
    
  • Fix platform conflicts.

    Error(s) prevent platform classification:

    Fix dependencies in pubspec.yaml.

  • The description is too short.

    Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 3 hints.

    Run flutter format to format lib/generated/i18n.dart.

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

    Similar analysis of the following files failed:

    • lib/src/streamed_store_connector.dart (hint)

Dependencies

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