brianegan/flutter_redux

onWillChange and onDidChange are not called after an action was dispatched in onInit

Nimrodda opened this issue · 0 comments

I'm experiencing an issue with version 0.6.0 where onWillChange and onDidChange are not called after I dispatched an action in onInit. I managed to reproduce this issue in a sample which I included below. The sample is the default counter app when creating a new Flutter project. I added print statements to the callbacks. Pressing the FAB will dispatch an increment action, which will call both onWillChange and onDidChange.

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

class AppState {
  final int counter;

  AppState(this.counter);
}

class Increment {}

AppState reduce(AppState state, dynamic action) {
  if (action is Increment) {
    return AppState(state.counter + 1);
  } else {
    return state;
  }
}

void main() {
  final store = Store<AppState>(
    reduce,
    initialState: AppState(0),
  );
  runApp(MyApp(store));
}

class MyApp extends StatelessWidget {
  final Store<AppState> store;

  MyApp(this.store);

  @override
  Widget build(BuildContext context) {
    return StoreProvider<AppState>(
      store: store,
      child: MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
          visualDensity: VisualDensity.adaptivePlatformDensity,
        ),
        home: MyHomePage(title: 'Flutter Demo Home Page'),
      ),
    );
  }
}

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

  final String title;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: StoreConnector<AppState, AppState>(
        converter: (store) => store.state,
        onInit: (store) {
          print('onInit');
          store.dispatch(Increment());
        },
        onWillChange: (previousViewModel, newViewModel) {
          print('onWillChange');
        },
        onDidChange: (viewModel) {
          print('onDidChange');
        },
        builder: (context, vm) {
          print('counter = ${vm.counter}');
          return Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  'You have pushed the button this many times:',
                ),
                Text(
                  '${vm.counter}',
                  style: Theme
                      .of(context)
                      .textTheme
                      .headline4,
                ),
              ],
            ),
          );
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => StoreProvider.of<AppState>(context).dispatch(Increment()),
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

The console output in this case without pressing the button is:

I/flutter ( 5625): onInit
I/flutter ( 5625): counter = 1

The following prints don't show up unless you press the button:

I/flutter ( 5625): onWillChange
I/flutter ( 5625): onDidChange
``