1

Where is the best place to manage routes in a Flutter App, when we use Redux? Because im follow some examples and some of them routing in actions and others in middlewares and even in containers.

Trect
  • 2,759
  • 2
  • 30
  • 35
Rubens Melo
  • 3,111
  • 15
  • 23

1 Answers1

0

You can dispatch the regular actions to trigger navigation.

class NavigationService {

 static NavigationService _service;

 NavigationService.__internal();

  factory NavigationService.instance(){
     if(_service == null){
     _service = new NavigationService.__internal();
   }
 return _service;
}

final GlobalKey<NavigatorState> _navigatorKey = GlobalKey<NavigatorState>();

Future<dynamic> pushNamed(String routeName, {dynamic arguments}) {
  return _navigatorKey.currentState.pushNamed(routeName, arguments:arguments);
}

bool pop() {
    return _navigatorKey.currentState.pop();
}

GlobalKey<NavigatorState> get navigatorKey => this._navigatorKey;
}

RouteMiddleware takes care of all the action related to routing

class RouteMiddleware<Action> extends TypedMiddleware<AppState, Action> {
  RouteMiddleware() : super(null);

  @override
  void call(Store<AppState> store, action, next) {
    if(action is YourRoutingAction){
      NavigationService.getInstance().pushedNamed(action.pageName);
    }
    next(action);
   }
}

Dispatch your action related to routing as your regular actions

goToHomePageAfterDataLoad(store) {
   dataLoadLoadLogic().then((data){
   store.dispatch(RoutingAction(pageName: "HOME"));
  });
}
user3555371
  • 238
  • 1
  • 7