1

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";
Rashorr
  • 51
  • 7

1 Answers1

1

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());
}});
Andy Joyce
  • 2,802
  • 3
  • 15
  • 24
  • Thanks for your Answer! I dont really understand how you access the argument. Do i have to do this in my view where i want to use the value? The MaterialPageRoute confuses me, sorry – Rashorr May 21 '21 at 16:52
  • No problem! Can you update your question with the code you have to display the SongListView? – Andy Joyce May 21 '21 at 16:55
  • I Edited it. This is my Code for my SongListView, I hope this is what you wanted haha. You can see i tried to access the EventName with widget.eventname but this obviously doesnt worked. – Rashorr May 21 '21 at 17:01
  • Ahh I mean the code that you have to navigate to the SongListView. Do you have a Router class? or have you set the onGenerateRoute property on your MaterialApp widget? – Andy Joyce May 21 '21 at 17:05
  • oh my bad haha! Yeah i have one i edited it :) – Rashorr May 21 '21 at 17:12
  • Thanks you very much! It works now. I'm sorry that I didn't understand it at the beginning. – Rashorr May 21 '21 at 18:00
  • No problem at all! Glad it worked for you! – Andy Joyce May 21 '21 at 18:04