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

Access store outside of Widget AND use Navigation outside of Widget (In Thunk Action for example) #184

Closed
pumuckelo opened this issue Jul 12, 2020 · 2 comments

Comments

@pumuckelo
Copy link

Hello,

I'm not sure if this is the right place to ask.
I couldn't make it to work to use the store.dispatch outside and use navigatorKey.currentState.push.... outside of a Widget.

To use the store outside of the Widgets, I create the store inside a separate file and pass it to the Store Provider, which wraps my main App.

--> Works

I also try to use a navigatorKey inside a navigationMiddleware. The navigatorKey is created in the same file as the store and is then passed as an argument to the navigationMiddleware.

Though, I'm getting this error

Tried calling: pushReplacementNamed<Object, Object>("/home", arguments: null)
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
#1 NavigationMiddleware.call (package:flutter_redux_navigation/src/navigation_middleware.dart:33:24)
#2 Store._createDispatchers.
package:redux/src/store.dart:255
#3 LoggingMiddleware.call
package:redux_logging/redux_logging.dart:120
#4 Store._createDispatchers.
package:redux/src/store.dart:255
#5 thunkMiddleware
package:redux_thunk/redux_thunk.dart:49
#6 Store._createDispatchers.
package:redux/src/store.dart:255
#7 Store.dispatch
package:redux/src/store.dart:267
#8 navigateAfterLogin.
package:socialcort_user_flutter/…/auth/helper.dart:61
#9 thunkMiddleware (p<…>

It seems like this is happening because the store is created outside of the main() function, not sure about that though.

Do you have any suggestions on how to use the store outside of widgets as well as using the navigation outside of widgets? For Example in middleware or in actions/helpers.

That would be really kind.

If you have any questions about the question, just hit me up. Thank you for the effort already!

@pumuckelo
Copy link
Author

Okay, I tried many different things now and changed my whole App structure to have my store inside the main method and access it in other files via Callbacks but I still get the same error.
What I found out is that the error is only caused by using navigatorKey in a middleware or outside of the Widget Tree.

My Code now looks like this:

`void main() {
final navigatorKey = new GlobalKey();
final Store store = Store(appStateReducer,
initialState: AppState.initial(),
middleware: [
thunkMiddleware,
LoggingMiddleware.printer(),
NavigationMiddleware(navigatorKey)
]);
successCallback(String email, String userId) {
store.dispatch(LoginSuccess(userId: userId, email: email));
store.dispatch(navigateAfterLogin());
}

errorCallback() {
store.dispatch(Logout());
}

runApp(EasyLocalization(
supportedLocales: [Locale("en", "US"), Locale("de", "DE")],
path: "assets/translations",
// assetLoader: JsonAssetLoader(),
fallbackLocale: Locale("en", "US"),
child: StoreProvider(
store: store,
child: MyApp(
authService: authService,
successCallback: successCallback,
errorCallback: errorCallback,
navigatorKey: navigatorKey,
),
)));
}

class MyApp extends StatelessWidget {
final FirebaseAuthService authService;
final successCallback;
final errorCallback;
final navigatorKey;

MyApp(
{this.authService,
this.successCallback,
this.errorCallback,
this.navigatorKey});

@OverRide
Widget build(BuildContext context) {
authService.startListeningForAuthState(successCallback, errorCallback);
return MaterialApp(
debugShowCheckedModeBanner: false,
localizationsDelegates: context.localizationDelegates,
supportedLocales: context.supportedLocales,
locale: context.locale,
title: 'Github',
navigatorKey: navigatorKey,
theme: MainTheme.data,
routes: {
Routes.loginScreen: () => LoginScreen(),
Routes.homeScreen: (
) => HomeScreen(),
Routes.signUpScreen: (_) => SignUpScreen(),
},
home: LoginScreen());
}
}`

@pumuckelo
Copy link
Author

Okay, ... after 7 hours I found the error, I was using StoreConnector without providing the types in a file that I didn't know I was already using the StoreConnector in.
That's regarding the Error: No StoreProvider found.

The solution for Tried calling: pushReplacementNamed<Object, Object>("/home", arguments: null) was to NOT dispatch actions before the navigatorKey is passed to the material app. Since I was using a firebase auth listener I was always dispatching before the navigatorKey was passed to the Material App.

My solution was to create a separate AuthHandler and pass this as a child to the MaterialApp.

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

No branches or pull requests

1 participant