Sorry, I misunderstand the scenario. Here is my suggest
remove routes
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,);
}
And change like
class RouteGenerator {
static Route<dynamic> generateRoute(RouteSettings settings) {
final args = settings.arguments;
print(args);
var routingData = settings.name;
switch (routingData) {
case "/main":
return MaterialPageRoute(
builder: (context) {
return Map_View();
},
settings: settings,
);
break;
case "/second":
return MaterialPageRoute(
builder: (context) {
return TaskClosed();
},
settings: settings,
);
break;
default:
return MaterialPageRoute(
builder: (context) {
return YouUnKnowPage();
},
settings: settings,
);
}
}
}
when you call Navigator.of(context).pushNamed("/main",arguments:"123");
It will move to TaskClosed and print 123 in the console
Furthermore, if you directly type the link like https:example.com/main?123
It will lead to YouUnKnowPage instead of Map_View and the arguments will be null. Try to use Navigator.of(context).pushNamed("/main",arguments:"123");
If you insist on directly type the link, you can try this
class RouteGenerator {
static Route<dynamic> generateRoute(RouteSettings settings) {
String routingData;
var arguments;
if (settings.name != null) {
routingData = settings.name;
}
final args = settings.arguments;
if (args != null) {
arguments = args;
} else {
Uri settingsUri = Uri.parse(settings.name);
if (settingsUri.hasQuery) {
arguments = "${settingsUri.queryParameters}";
}
if (settingsUri.pathSegments.length > 1) {
routingData =
"/" + settingsUri.pathSegments[settingsUri.pathSegments.length - 1];
}
}
if (arguments != null) {
print(arguments);
}
switch (routingData) {
case "/main":
return MaterialPageRoute(
builder: (context) {
return Map_View();
},
settings: settings,
);
break;
case "/second":
return MaterialPageRoute(
builder: (context) {
return TaskClosed();
},
settings: settings,
);
break;
default:
return MaterialPageRoute(
builder: (context) {
return YouUnKnowPage();
},
settings: settings,
);
}
}
}
Now it will move to TaskClosed and print {123:} in the console
Navigator.pushNamed(context, '/second', arguments: 'https:example.com/main?123')params/:idyou will get the id