How can a named route have URL parameters in flutter web?
tl;dr
//in your example: settings.name = "/post?id=123"final settingsUri = Uri.parse(settings.name);//settingsUri.queryParameters is a map of all the query keys and valuesfinal postID = settingsUri.queryParameters['id'];print(postID); //will print "123"
Drilldown
In a perfect world you would access queryParameters
with Uri.base.queryParameters
because:
Returns the natural base URI for the current platform. When running in a browser this is the current URL of the current page (from window.location.href). When not running in a browser this is the file URI referencing the current working directory.
But currently there is an issue in flutter where you have #/
in your path which messes the Uri.base
interpretation of the Uri.
Follow the issue #33245 until this matter is addressed and you will be able to use Uri.base.queryParameters
please follow this link further information https://flutter.dev/docs/cookbook/navigation/navigate-with-arguments
on your MaterialApp
onGenerateRoute: (settings) { // If you push the PassArguments route if (settings.name == PassArgumentsScreen.routeName) { // Cast the arguments to the correct type: ScreenArguments. final ScreenArguments args = settings.arguments; // Then, extract the required data from the arguments and // pass the data to the correct screen. return MaterialPageRoute( builder: (context) { return PassArgumentsScreen( title: args.title, message: args.message, ); },
or you can nativate like web using this plugin fluro
I'm new to Flutter, and I found a quirky workaround,...
import 'dart:html';String itemID;//My url looks like this,... http://localhost:57887/#item_screen/12345//Counted 13 characters for '#item_screen/' then got the substring as below itemID = window.location.hash.substring(13); print(itemID) //12345
Not very sophisticated, but worked :-D