Giter Site home page Giter Site logo

Comments (8)

xqwzts avatar xqwzts commented on July 19, 2024 165

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.

brianegan avatar brianegan commented on July 19, 2024 12

@temirfe You have two options:

  1. Make the navigatorKey a global variable (probably wouldn't do this myself)
  2. 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.

temirfe avatar temirfe commented on July 19, 2024 1
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.

xqwzts avatar xqwzts commented on July 19, 2024

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.

MichaelRFairhurst avatar MichaelRFairhurst commented on July 19, 2024

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.

brianegan avatar brianegan commented on July 19, 2024

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.

MichaelRFairhurst avatar MichaelRFairhurst commented on July 19, 2024

I guess there's no reason to leave this open. Thanks for the help, everyone!

from flutter_redux.

sjmcdowall avatar sjmcdowall commented on July 19, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.