flutternavigationnavigationservice

Flutter accessing a Value that i passed from my NavigationService


I have a NavigationService with which I pass a value to another screen. But i can´t really access this value i passed to the new Screen.

My NavigationService:

class NavigationService {
  final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

  Future<dynamic> navigateTo(String routeName, String eventName) {
    return navigatorKey.currentState.popAndPushNamed(routeName);
  }

  goBack() {
    navigatorKey.currentState.pop();
  }
}

The line in which i pass the Value:

actions: <Widget>[
                    TextButton(
                      child: const Text('Submit'),
                      onPressed: () {
                        textfieldController.text = eventName;
                        Navigator.pop(context);
                      
                        locator<NavigationService>()
                            .navigateTo(SonglistRoute, eventName);
                      },

And the screen in which I take the value:

class SongListView extends StatelessWidget {
  final String eventName;
  const SongListView({Key key, this.eventName}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ScreenTypeLayout(
      mobile: SonglistViewMobile(eventName: eventName),
      desktop: SonglistViewDesktop(eventName: eventName),
    );
  }
}
---- new File----
class SonglistViewDesktop extends StatefulWidget {
  final String eventName;
  SonglistViewDesktop({Key key, this.eventName}) : super(key: key);

  @override
  _songlistState createState() => _songlistState();
---i wont post my build widget here because its too much----
}

I cant access the eventName Value in my Build Widget.

Edit!!!!

Router class

Route<dynamic> generateRoute(RouteSettings settings) {
  print('generateRoute: ${settings.name}');
  switch (settings.name) {
    case HomeRoute:
      return _getPageRoute(HomeView());
    case HelpRoute:
      return _getPageRoute(HelpView());
    case AboutRoute:
      return _getPageRoute(AboutView());
    case EnterRoute:
      return _getPageRoute(EnterView());
    case SonglistRoute:
      return _getPageRoute(SongListView());
    default:
      return _getPageRoute(HomeView());
  }
}

PageRoute _getPageRoute(Widget child) {
  return PageRouteBuilder(
    pageBuilder: (context, animation, secondaryAnimation) => child,
    transitionsBuilder: (context, animation, secondaryAnimation, child) {
      var begin = Offset(0.0, 1.0);
      var end = Offset.zero;
      var curve = Curves.ease;

      var tween = Tween(begin: begin, end: end).chain(CurveTween(curve: curve));

      return SlideTransition(
        position: animation.drive(tween),
        child: child,
      );
    },
  );
}

const String HomeRoute = "home";
const String AboutRoute = "about";
const String HelpRoute = "help";
const String EnterRoute = "Enter";
const String SonglistRoute = "Songlist";

Solution

  • You aren't doing anything with the eventName. You need to add it as an argument when you call popAndPushNamed.

    navigatorKey.currentState.popAndPushNamed(routeName, arguments: eventName);
    

    I assume that you are using generatedRoutes so to access the argument you could do something like this

    Route<dynamic> generateRoute(RouteSettings settings) {
    print('generateRoute: ${settings.name}');
      switch (settings.name) {
        case HomeRoute:
          return _getPageRoute(HomeView());
        case HelpRoute:
          return _getPageRoute(HelpView());
        case AboutRoute:
          return _getPageRoute(AboutView());
        case EnterRoute:
          return _getPageRoute(EnterView());
        case SonglistRoute:
          return _getPageRoute(SongListView(eventName: settings.argument as String));
        default:
          return _getPageRoute(HomeView());
    }});