Comments (8)
FYI you can access your app's Navigator without a context
by setting the navigatorKey
property of your MaterialApp
:
/// A key to use when building the [Navigator].
///
/// If a [navigatorKey] is specified, the [Navigator] can be directly
/// manipulated without first obtaining it from a [BuildContext] via
/// [Navigator.of]: from the [navigatorKey], use the [GlobalKey.currentState]
/// getter.
///
/// If this is changed, a new [Navigator] will be created, losing all the
/// application state in the process; in that case, the [navigatorObservers]
/// must also be changed, since the previous observers will be attached to the
/// previous navigator.
final GlobalKey<NavigatorState> navigatorKey;
Create the key:
final GlobalKey<NavigatorState> navigatorKey = new GlobalKey<NavigatorState>();
Pass it to MaterialApp:
new MaterialApp(
title: 'MyApp',
onGenerateRoute: generateRoute,
navigatorKey: key,
);
Push routes:
navigatorKey.currentState.pushNamed('/someRoute');
from flutter_redux.
@temirfe You have two options:
- Make the
navigatorKey
a global variable (probably wouldn't do this myself) - Create a Middleware that accepts the navigatorKey (my preferred mechanism)
In code using a MiddlewareClass
:
class MyMiddleware extends MiddlewareClass<AppState> {
final GlobalKey<NavigatorState> navigatorKey;
MyMiddleware(this.navigatorKey);
@override
void call(Store<AppState> store, dynamic action, NextDispatcher next) {
if (action is SomeAction) {
navigatorKey.currentState.pushNamed('/someRoute');
}
}
}
To provide this to your Store:
void main() {
final navigatorKey = new GlobalKey<NavigatorState>();
final store = Store(myReducer, middleware: [MyMiddleware(navigatorKey)]);
runApp(StoreProvider(store: store, child: MaterialApp(navigatorKey: navigatorKey)));
}
from flutter_redux.
navigatorKey.currentState.pushNamed('/someRoute');
it would be nice if you also showed how did you get reference to navigatorKey in navigatorKey.currentState.pushNamed('/someRoute');
because in my case editor is complaining that navigatorKey is undefined
from flutter_redux.
I find it preferable to use this to navigate in a side-effect action/middleware and leave navigation state handling to flutter.
from flutter_redux.
ah, awesome! Totally didn't look deeper at the navigation API one bit. HUGE thanks, that at the very least unblocks me!
Is there any documentation on how to write a middleware? That's another thing I didn't see. Middleware does indeed seems like the right place for this.
from flutter_redux.
Thanks for writing in and to xqwzts for the good answer! On vacation for a few more days, but you can find some docs on Middleware here: https://github.com/johnpryan/redux.dart/blob/master/doc/async.md
from flutter_redux.
I guess there's no reason to leave this open. Thanks for the help, everyone!
from flutter_redux.
If anyone is still reading this -- I am trying this but it's not working all that well -- when I get an outside event .. and want to go to a particular screen .. it goes there -- but loses ALL context such as theme / backbitten / app bar stuff / etc. It's a plain screen and no way to navigate ..
This is an existing screen that is navigable via App Bar buttons, etc. So ... how can this work?
Cheers
from flutter_redux.
Related Issues (20)
- How to not accept change notifications when the Router is not at the top level, and then take the initiative to obtain the changes when the Router returns to the top level HOT 1
- onDidChange called twice HOT 8
- Is this package still actively maintained?
- `onWillChange` and `onDidChange` errors are swallowed HOT 4
- Warning related to `?` operator on Linux/Dev channel. HOT 2
- Import Store data type
- Widgets binding error after upgrading flutter version to 2.13.0-0.1.pre HOT 1
- onDidChange not working as expected HOT 3
- Build warning with Flutter 3.0 HOT 5
- flutter_redux depends on flutter_redux, version solving failed HOT 3
- onWillChange and onDidChange called twice after state update HOT 2
- Passing Store as an argument to MethodChannel and Background Isolate
- Question on how to use the reducer right way HOT 2
- [Question]: Any thoughts on difference between ignoreChange and distinct?
- Socket and Flutter_redux HOT 18
- Dispatching an action that does not alter state rerenders the widget, causing an infinite loop HOT 5
- How to watch redux store change inside useEffect? HOT 5
- [Question] is it expected that the store doesn't run until frames render? HOT 1
- After push a new page, and Dispatching an action, there is an infinite loop
- Is flutter redux actively maintained HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flutter_redux.