Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After rebuild callbacks #55

Merged
merged 4 commits into from
Jun 19, 2018
Merged

After rebuild callbacks #55

merged 4 commits into from
Jun 19, 2018

Conversation

brianegan
Copy link
Owner

This adds two callbacks to the StoreConnector and StoreBuilder:

  • onInitialBuild -- Called after the Widget is built the first time
  • onDidChange -- Called after the Widget is rebuilt when the ViewModel changes (if you use distinct and the ViewModel does not change, this will not be invoked).

Fixes #47

These allow users to run code after the build method is called the first
time, and after the widget is rebuilt after the ViewModel changes
@codecov
Copy link

codecov bot commented Jun 12, 2018

Codecov Report

Merging #55 into master will increase coverage by 0.17%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #55      +/-   ##
==========================================
+ Coverage   98.59%   98.76%   +0.17%     
==========================================
  Files           1        1              
  Lines          71       81      +10     
==========================================
+ Hits           70       80      +10     
  Misses          1        1
Impacted Files Coverage Δ
lib/flutter_redux.dart 98.76% <100%> (+0.17%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a8802ec...6b71a02. Read the comment docs.

@kitoko552
Copy link

thank you for your changes:)
it seems to have no problem and looks great!

@brianegan
Copy link
Owner Author

Sounds good! I'm currently traveling and will merge this up when I'm back in action early next week :)

If you need this right away, please consider pointing your pubspec to this branch until I can publish a new version!

@AlexandreRoba
Copy link

Hi Brian,

Thanks for the quick implementation :).
I tried to use it but i'm having an exception when using the Navigator inside onDidChange.

flutter: ══╡ EXCEPTION CAUGHT BY SCHEDULER LIBRARY ╞═════════════════════════════════════════════════════════
flutter: The following assertion was thrown during a scheduler callback:
flutter: Looking up a deactivated widget's ancestor is unsafe.
flutter: At this point the state of the widget's element tree is no longer stable. To safely refer to a
flutter: widget's ancestor in its dispose() method, save a reference to the ancestor by calling
flutter: inheritFromWidgetOfExactType() in the widget's didChangeDependencies() method.
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #0      Element._debugCheckStateIsActiveForAncestorLookup.<anonymous closure> (package:flutter/src/widgets/framework.dart:3203:9)
flutter: #1      Element._debugCheckStateIsActiveForAncestorLookup (package:flutter/src/widgets/framework.dart:3212:6)
flutter: #2      Element.ancestorStateOfType (package:flutter/src/widgets/framework.dart:3254:12)
flutter: #3      Navigator.of (package:flutter/src/widgets/navigator.dart:1176:19)
flutter: #4      Navigator.pushReplacementNamed (package:flutter/src/widgets/navigator.dart:684:22)
flutter: #5      SplashScreen.build.<anonymous closure> (package:sweetnest_app/containers/splash_screen.dart:23:21)
flutter: #6      _StoreStreamListenerState._init.<anonymous closure>.<anonymous closure> (package:flutter_redux/flutter_redux.dart)
flutter: #7      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:990:15)
flutter: #8      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:938:9)
flutter: #9      _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:842:5)
flutter: #10     _invoke (dart:ui/hooks.dart:120:13)
flutter: #11     _drawFrame (dart:ui/hooks.dart:109:3)
flutter: ════════════════════════════════════════════════════════════════════════════════════════════════════
flutter: Another exception was thrown: Looking up a deactivated widget's ancestor is unsafe.

the exception is raised on a container splash screen. It's store connector looks like that:

  @override
  Widget build(BuildContext context) {
    return StoreConnector<AppState, _ViewModel>(
      converter: _ViewModel.fromStore,
      onInitialBuild: (_ViewModel vm) {
        vm.onLaunchStarted();
      },
      onDidChange: (_ViewModel vm){
        if(vm.initializationCompleted)
          Navigator.pushReplacementNamed(context, 'home');
      },
      builder:...
}

onLauchStarted dispatch an action that is captured by a middleware that perform all necessary initialization. Once the middleware is complete it dispatch an action that will be reduce to set a initializationCompleted flag on the appState.
On onDidChange I check for this state and if its true I navigate to the home screen...

The flow works but I have this nasty exception... Any idea?

thanks ,

Alex

@AlexandreRoba
Copy link

I moved the conditional routing to onWillChange and the exception is no more.
I guess we should not make use of the context on the onDidChange?

@brianegan
Copy link
Owner Author

Thanks for the feedback @AlexandreRoba -- I'll add that to the docs.

@brianegan brianegan merged commit bf28d66 into master Jun 19, 2018
@brianegan brianegan deleted the after-rebuild-callbacks branch June 19, 2018 11:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants