Flutter 访问我从 NavigationService 传递的值

Flutter accessing a Value that i passed from my NavigationService

我有一个 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();
  }
}

我传递值的行:

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

以及我取值的屏幕:

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----
}

我无法访问我的构建小部件中的 eventName 值。

编辑!!!!

路由器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";

您没有对 eventName 做任何事情。您需要在调用 popAndPushNamed 时将其添加为参数。

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

我假设您使用的是 generatedRoutes,因此要访问参数,您可以这样做

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());
}});