built_viewmodel_flutter_redux 1.2.2

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

built_viewmodel.dart

The built_viewmodel.dart package provides a way to create ViewModel classes. It is a little bit inspired by the Android ViewModel but just a little bit ;-)

Usually, you call setState() whenever the state of the Widget should change. But in my opinion, it is not a good idea to call setState() when just a small part of the whole state has changed (read more: How fast is Flutter? I built a stopwatch app to find out., Avoiding Empty State Callbacks).

This package creates lots of Streams (as much as you need) to feed StreamBuilders.

Let's start

Create an abstract class, e.g. MyHomePageViewModel:

library main;

import 'dart:async';

import 'package:built_viewmodel/built_viewmodel.dart';

part 'main.g.dart';

abstract class MyHomePageViewModel implements ViewModel<MyHomePageViewModelController> {


  factory MyHomePageViewModel() = _$MyHomePageViewModel;
  MyHomePageViewModel._();
}

And add a reference of it to the Widget:

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final MyHomePageViewModel model = new MyHomePageViewModel();
  final String title;

  @override
  _MyHomePageState createState() {
    model.setCounter(0);
    return new _MyHomePageState();
  }
}

For now, the ViewModel makes nothing. You have to add your needed stream(s):

abstract class MyHomePageViewModel implements ViewModel<MyHomePageViewModelController> {

  Stream<int> get counter;   // new

  factory MyHomePageViewModel() = _$MyHomePageViewModel;
  MyHomePageViewModel._();

You also need to add a code generator. Create a tool/build.dart file with the following content:

import 'dart:async';

import 'package:build_config/build_config.dart';
import 'package:build_runner/build_runner.dart';
import 'package:built_viewmodel_generator/built_viewmodel_generator.dart';
import 'package:source_gen/source_gen.dart';

final builders = [
  applyToRoot(
    new PartBuilder([
      new BuiltViewModelGenerator(),
    ]),
    generateFor: const InputSet(
      include: const [
        'lib/*.dart',
      ],
    ),
  ),
];

Future main(List<String> args) async {
  await build(
    builders,
    deleteFilesByDefault: true,
    verbose: false,
  );
}

Execute the tool/build.dart file and it will generate a main.g.dart file which has all the logic for your stream(s).

Use the stream

To use the stream(s) you need a StreamBuilder where you have to set the stream to the one from the ViewModel:

new StreamBuilder(
  stream: widget.model.counter,
  builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
    if (snapshot.hasError) return new Text('Error: ${snapshot.error}');
    switch (snapshot.connectionState) {
      case ConnectionState.none:
        return new Text('not connected');
      case ConnectionState.waiting:
        return new Text('awaiting interaction...');
      case ConnectionState.active:
        return new Text('counter: ${snapshot.data}');
      case ConnectionState.done:
        return new Text('counter: ${snapshot.data} (closed)');
      default:
        throw "Unknown: ${snapshot.connectionState}";
    }
  },
),

Whenever you send a new value to the stream the content of the StreamBuilder will change but only this small part and not the whole state.

To do that add a void setCounter(int value) method to the ViewModel ...

abstract class MyHomePageViewModel implements ViewModel<MyHomePageViewModelController> {
  Stream<int> get counter;

  void setCounter(int value) => controller.counter.add(value);   // new

  factory MyHomePageViewModel() = _$MyHomePageViewModel;
  MyHomePageViewModel._();
}

... and just call it.

Don't forget ...

... to call dispose()

Add this to your State class

@override
void dispose() {
  widget.model.dispose();
  super.dispose();
}

Advanced feature

Sometimes you need to know the state of the stream, for example, to start a download.

You can create methods and annotate them with one of this annotations: @OnListenHandler, @OnPauseHandler, @OnResumeHandler, @OnCancelHandler. These annotations have a name parameter which must match with the name of the stream.

For example, if you have a counter stream Stream<int> get counter, and you want to know when something is listening for it the annotation should look like this: @OnListenHandler('counter').

abstract class MyHomePageViewModel implements ViewModel<MyHomePageViewModelController> {
  Stream<int> get counter;

  @OnListenHandler('counter')                           // new
  onListen() => print('start listening for updates');   // new

  void setCounter(int value) => controller.counter.add(value);

  factory MyHomePageViewModel() = _$MyHomePageViewModel;

  MyHomePageViewModel._();
}

You can find the full example here.

1.2.1 - 2018/04/10

  • small enhancements

1.2.0 - 2018/04/09

1.1.1 - 2018/04/04

  • handle import prefixes
  • optimise code generator to pass static analysis check

1.1.0 - 2018/04/03

  • API breaking change: rename listener classes to match Dart naming guide

1.0.1 - 2018/04/02

  • small enhancements

1.0.0 - 2018/04/01

  • first release

example/lib/main.dart

// Copyright (c) 2018., Ralph Bergmann.
// All rights reserved. Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

library main;

import 'dart:async';

import 'package:built_viewmodel/built_viewmodel.dart';
import 'package:built_viewmodel_flutter_redux/built_viewmodel_flutter_redux.dart';
import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:redux/redux.dart';

part 'main.g.dart';

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

class IncrementAction {}

class AppState {
  final int count;

  AppState(this.count);

  factory AppState.initial() => AppState(0);
}

AppState appReducer(AppState state, dynamic action) => AppState(counterReducer(state.count, action));

final Reducer<int> counterReducer = combineReducers([
  TypedReducer<int, IncrementAction>(_increment),
]);

int _increment(int count, IncrementAction action) => count + 1;

class MyApp extends StatelessWidget {
  final Store<AppState> store = Store<AppState>(
    appReducer,
    initialState: AppState.initial(),
  );

  Widget build(BuildContext context) => StoreProvider(
        store: store,
        child: MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(primarySwatch: Colors.blue),
          home: new MyHomePage(title: 'Flutter Demo Home Page'),
        ),
      );
}

class MyHomePage extends StatelessWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final MyHomePageViewModel model = new MyHomePageViewModel();
  final String title;

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(title),
      ),
      body: new ViewModelConnector(
        viewModel: model,
        onInit: (MyHomePageViewModel viewModel) => viewModel.setCounter(0),
        builder: (MyHomePageViewModel viewModel) => new Center(
              child: new Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  new Text('You have pushed the button this many times:'),
                  new StreamBuilder(
                    stream: viewModel.counter,
                    builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
                      if (snapshot.hasError) return new Text('Error: ${snapshot.error}');
                      switch (snapshot.connectionState) {
                        case ConnectionState.none:
                          return new Text('not connected');
                        case ConnectionState.waiting:
                          return new Text('awaiting interaction...');
                        case ConnectionState.active:
                          return new Text('counter: ${snapshot.data}');
                        case ConnectionState.done:
                          return new Text('counter: ${snapshot.data} (closed)');
                        default:
                          throw "Unknown: ${snapshot.connectionState}";
                      }
                    },
                  ),
                ],
              ),
            ),
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: () => model.increaseCounter(),
        tooltip: 'Increment',
        child: new Icon(Icons.add),
      ),
    );
  }
}

abstract class MyHomePageViewModel extends ViewModel<MyHomePageViewModelController>
    with ViewModelFlutterReduxMixin<AppState, int> {
  Stream<int> get counter;

  @OnListenHandler('counter')
  void onListen() => print('start listening');

  @OnPauseHandler('counter')
  void onPause() => print('pause listening');

  @OnResumeHandler('counter')
  void onResume() => print('resume listening');

  @OnCancelHandler('counter')
  void onCancel() => print('cancel listening');

  void setCounter(int value) => controller.counter.add(value);

  void increaseCounter() => store?.dispatch(new IncrementAction());

  int convert(AppState state) => state.count;

  void onStateChanged(int state) => setCounter(state);

  factory MyHomePageViewModel() = _$MyHomePageViewModel;

  MyHomePageViewModel._();
}

Use this package as a library

1. Depend on it

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


dependencies:
  built_viewmodel_flutter_redux: ^1.2.2

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:built_viewmodel_flutter_redux/built_viewmodel_flutter_redux.dart';
  
Version Uploaded Documentation Archive
1.2.2 Apr 12, 2018 Go to the documentation of built_viewmodel_flutter_redux 1.2.2 Download built_viewmodel_flutter_redux 1.2.2 archive
1.2.0 Apr 9, 2018 Go to the documentation of built_viewmodel_flutter_redux 1.2.0 Download built_viewmodel_flutter_redux 1.2.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
0
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]
0
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.

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 >=2-0-0-dev <2.0.0