I'm working on a flutter application mainly for the web, and I'm unable to add/get the query parameter from the URL, the query parameter will contain an id, and this id should be used inside the app
this my route setup on my app state:
return MaterialApp(navigatorKey: key, initialRoute: '/main',
routes: {
// When navigating to the "/" route, build the FirstScreen widget.
'/main': (context) => Map_View(),
// When navigating to the "/second" route, build the SecondScreen widget.
'/second': (context) => TaskClosed(),
},onGenerateRoute: RouteGenerator.generateRoute,);
}
class RouteGenerator{
static Route<dynamic> generateRoute(RouteSettings settings){
final args = settings.arguments;
print(args);
var routingData = settings.name;
}}
the settings.arguments are always null
so what should I pass to initialRoute to make it accept arguments on the first screen for example, the calling URL should be like this:
https:example.com/main?123
so how to get this parameter from the URL
I tried this code:
class RouteGenerator {
static Route<dynamic> generateRoute(RouteSettings settings) {
String? route;
Map? queryParameters;
if (settings.name != null) {
var uriData = Uri.parse(settings.name!);
route = uriData.path;
queryParameters = uriData.queryParameters;
}
var message =
'generateRoute: Route $route, QueryParameters $queryParameters';
print(message);
return MaterialPageRoute(
builder: (context) {
return MyHomePage(title: message);
},
settings: settings,
);
}
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Test App',
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute: '/main',
routes: {
// '/': (context) {
// print('route: /');
// return MyHomePage(title: 'Home');
// },
'/main': (context) {
print('route: /main');
return MyHomePage(title: 'route: Main');
},
'/second': (context) {
print('route: /second');
return MyHomePage(title: 'route: Second');
},
},
onGenerateRoute: RouteGenerator.generateRoute,
);
}
}
When I run the app with the URL http://localhost:55260/#/main?123
, I get this output:
generateRoute: Route /, QueryParameters {}
generateRoute: Route /main, QueryParameters {123: }
The screen is displayed for /main
and the URL is displayed correctly.