stream_mixin 1.0.6

  • Readme
  • Changelog
  • Example
  • Installing
  • new55

stream_mixin #

A simple mixin for adding a stream behaviour to any class object.

Intention #

The intention for this package is, instead of using state, we can use this package for streaming data from our controller/adapter/service to the widgets, which will result in better performance and cleaner code.

Getting Started #

To subscribe a class instance, create the class with StreamMixin

Create a service class #

class AppTagService with StreamMixin<String> {
  AppTagService._();
  static final AppTagService instance = AppTagService._();
}

Subscribe to change of the tag service #

class CurrentTag extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return StreamBuilder<String>(
      builder: (context, snap) => Text(snap.data ?? 'No tag selected yet.'),
      stream: AppTagService.instance.onChange, //⭐
    );
  }
}

Change current tag #

Note, tag can be changed from any place in the app because it does not need context or state.

someFunction() {
  AppTagService.instance.update(element: 'COVID-19');
}

Check example folder for more examples #

PS #

[1.0.6] - 21 June 2020. #

  • added descriptive comments for classes, fields and methods

[1.0.5] - 21 June 2020. #

  • updated issue link in readme

[1.0.4] - 20 June 2020. #

  • merged examples in main.dart

[1.0.3] - 20 June 2020. #

  • moved todo example in separate file

[1.0.2] - 20 June 2020. #

Bux fixes: #

  • null id in example todo model
  • fixed add method in StoreService
  • added updateAll in StoreService
  • changed item in StreamMixin to element
  • made element in StreamMixin optional
  • updated tests for above changes

[1.0.1] - 20 June 2020. #

  • Added intention in readme.

[1.0.0] - 20 June 2020. #

  • Initial Release.

example/main.dart

import 'package:flutter/material.dart';
import 'package:stream_mixin/store_service.dart';
import 'package:stream_mixin/stream_mixin.dart';

// Example 1

class AppTagService with StreamMixin<String> {
  AppTagService._();
  static final AppTagService instance = AppTagService._();
}

class CurrentTag extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return StreamBuilder<String>(
      builder: (context, snap) => Text(snap.data ?? 'No tag selected yet.'),
      stream: AppTagService.instance.onChange, //⭐
    );
  }
}

someFunction() {
  AppTagService.instance.update(element: 'COVID-19');
}

// Example 2

class TodoModel extends BasicModel {
  String title;
  bool completed;

  TodoModel({
    int id,
    @required this.title,
    this.completed: false,
  }) {
    this.id = id ?? DateTime.now().millisecondsSinceEpoch;
  }
}

class TodoService extends StoreService<TodoModel> {
  TodoService._();
  static TodoService store = TodoService._();

  List<TodoModel> get accending {
    var todos = this.values;
    todos.sort((a, b) => a.id - b.id);
    return todos;
  }

  List<TodoModel> get completed {
    return this.values.where((todo) => todo.completed).toList();
  }

  List<TodoModel> get pending {
    return this.values.where((todo) => !todo.completed).toList();
  }

  void toggleStatus(TodoModel todo) {
    todo.completed = !todo.completed;
    TodoService.store.update(
      element: AppStreamElement(item: todo, operation: Operation.Update),
    );
  }
}

class TodoListWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
      builder: (context, snap) => ListView(
        children: TodoService.store.accending.map(_buildListTile).toList(),
      ),
      stream: TodoService.store.onChange,
    );
  }

  ListTile _buildListTile(TodoModel todo) {
    return ListTile(
      leading: Text(todo.id.toString()),
      title: Text(
        todo.title,
        style: TextStyle(
          decoration:
              todo.completed ? TextDecoration.lineThrough : TextDecoration.none,
        ),
      ),
      trailing: Checkbox(
        value: todo.completed,
        onChanged: (bool _) {
          TodoService.store.toggleStatus(todo);
        },
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  stream_mixin: ^1.0.6

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:stream_mixin/stream_mixin.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
9
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
55
Learn more about scoring.

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

  • Dart: 2.8.4
  • pana: 0.13.13
  • Flutter: 1.17.5

Analysis suggestions

Package not compatible with SDK dart

because of import path [stream_mixin] that is in a package requiring null.

Health suggestions

Format lib/store_service.dart.

Run flutter format to format lib/store_service.dart.

Format lib/stream_mixin.dart.

Run flutter format to format lib/stream_mixin.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
meta ^1.1.8 1.1.8
Transitive dependencies
collection 1.14.12 1.14.13
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8
Dev dependencies
flutter_test