英文:
how to launch a web url and extract the id for its using flutter web app
问题
考虑在我的Web应用程序中的http://example。我的主机网址是ae。
我为每个商店提供一个QR码,当他们扫描它时,他们会看到类似http://example.ae/a23bc的内容,其中包含商店的唯一标识符。
当有人扫描并启动Web应用程序时,他们会收到404错误,因为未找到该路由。
我需要从地址URL中提取用户的ID,当他们登录时,我只想启动基本的URL,忽略用户的ID。我希望使用API生成和表示数据,所以ID只用于提取和使用数据。
有人知道在Flutter Web中实现这一目标的最佳方法吗?
启动基本URL http://exapmle.ae
忽略ID
并且我想将ID保存下来以供进一步使用。
英文:
Consider http://example in my web application.My host url is ae.
I give each store a QR code, and when they scan it, they will see something like http://example.ae/a23bc, which includes the store's unique identifier.
When someone scans and launches a web application, they receive a 404 error because the route was not found.
I need to extract the user's id from the address url when they login, and I simply want to launch the basic url while ignoring the user's id. I want to produce and represent data using an API, so id is simply for extracting and using this.
any one know the best way to do this in Flutter Web
lauch the base url http"//exapmle.ae
ignoring the id
and i want to save the id in my for further functioning
答案1
得分: 0
你应该设置路由如下:
http://example.ae/home?uniqueId=yourUniqueId
你还应该使用 MaterialApp 的 onGenerateRoute: generateRoute
,以解析访问的网页中的查询参数,就像这样:
Route<dynamic> generateRoute(RouteSettings settings) {
RoutingData routingData = settings.name!.getRoutingData;
switch (routingData.route) {
case AppRoutes.homeRoute:
return _getPageRoute(
HomeView(
homeController: sl(),
),
settings
);
case AppRoutes.blog:
return _getPageRoute(const ViewBlogs(), settings);
case AppRoutes.blogPost:
log("Going Blog Post ${routingData.route} post id ${routingData['id']}");
return _getPageRoute(
BlogDetailPage(
postId: routingData['id'],
),
settings
);
}
}
这是路由数据类:
class RoutingData {
final String route;
final Map<String, String> _queryParameters;
RoutingData({
required this.route,
required Map<String, String> queryParameters,
}) : _queryParameters = queryParameters;
operator [](String key) => _queryParameters[key];
}
并且使用 String 类的扩展,可以从 settings.name 中获取 RoutingData:
extension StringExtension on String {
RoutingData get getRoutingData {
var uriData = Uri.parse(this);
return RoutingData(
queryParameters: uriData.queryParameters,
route: uriData.path,
);
}
}
英文:
you should set the route like this
http://example.ae/home?uniqueId=yourUniqueId
you should also use the
onGenerateRoute: generateRoute,
of the MaterialApp in order to parse the queryParameters from the visited web page, like this
Route<dynamic> generateRoute(RouteSettings settings) {
RoutingData routingData = settings.name!.getRoutingData;
switch (routingData.route) {
case AppRoutes.homeRoute:
return _getPageRoute(
HomeView(
homeController: sl(),
),
settings);
case AppRoutes.blog:
return _getPageRoute(const ViewBlogs(), settings);
case AppRoutes.blogPost:
log("Going Blog Post ${routingData.route} post id
${routingData['id']}");
return _getPageRoute(
BlogDetailPage(
postId: routingData['id'],
),
settings);
this being the routing data class
class RoutingData {
final String route;
final Map<String, String> _queryParameters;
RoutingData({
required this.route,
required Map<String, String> queryParameters,
}) : _queryParameters = queryParameters;
operator [](String key) => _queryParameters[key];
}
and using a extension on the String class, of the settings.name you can get the RoutingData
extension StringExtension on String {
RoutingData get getRoutingData {
var uriData = Uri.parse(this);
// print('queryParameters: ${uriData.queryParameters} path:
${uriData.path}');
return RoutingData(
queryParameters: uriData.queryParameters,
route: uriData.path,
);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论