navigation to detailscreen with data
Navigator.of(context).pushNamed(‘/detailScren’, arguments: "data");
DetailScreen: here i’m getting null from Modal Routes.
class DetailScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
final arguments = ModalRoute.of(context)!.settings.arguements;
print(arguments); // always null
return Scaffold(
body: Container(),
);
}
Below is App code in main.dart
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: context.localizationDelegates,
supportedLocales: context.supportedLocales,
locale: context.locale,
title: Strings.appName,
theme: CustomTheme.lightTheme,
onGenerateRoute: AppRoutes.generateRoute,
home: MainScreen(),
);
}
Arguments passing and receiving.
2
Answers
You wrote "ModalRoute.of(context)!.settings.arguements;", arguements, instead of arguments.
The
onGenerateRoute
approach is different from passing arguments directly to the route. You should manage arguments directly insideonGenerateRoute
: https://docs.flutter.dev/cookbook/navigation/navigate-with-arguments#alternatively-extract-the-arguments-using-ongenerateroute