有没有人遇到过使用 Web url 直接路由到已知路由的问题。我已经在当前稳定版(2.5.3)中使用非常小的颤振应用程序复制了这一点。举一个简单的路由示例:
initialRoute: '/login',
routes: {
'/login': (context) => LoginScreen(),
},
onGenerateRoute: (RouteSettings routeSettings) {
if (routeSettings.name == LoginScreen.routeName) {
return MaterialPageRoute(
settings: routeSettings,
builder: (context) => LoginScreen(),
);
}
},
onUnknownRoute: (RouteSettings routeSettings) {
return MaterialPageRoute(
settings: routeSettings,
builder: (context) => Scaffold(
body: Center(
child: Text('404!'),
),
),
);
},
加载应用程序后,它会按预期运行,并毫无例外地直接进入登录屏幕。如果您输入未知路线,我会看到 404 not found scaffold 。如果您打开一个新选项卡并粘贴完全相同的网址,即 http://localhost:5000/#/login 我得到:
Could not navigate to initial route.
The requested route name was: "/login"
There was no corresponding route in the app, and therefore the initial route specified will be ignored and "/" will be used instead.
这个问题是我试图通过另一个应用程序中的 webview 访问已知路由并传递 url 参数,并且总是遇到这个问题。有没有办法捕获这个事件,因为 onUnknownRoute 和 onGenerateRoute 都没有被调用并且导航恢复到“/”。
我的flutter doctor -v
输出:
[✓] Flutter (Channel stable, 2.5.3, on macOS 11.6 20G165 darwin-x64, locale en-US)
• Flutter version 2.5.3 at /Users/asdf/development/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 18116933e7 (2 weeks ago), 2021-10-15 10:46:35 -0700
• Engine revision d3ea636dc5
• Dart version 2.14.4
[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
• Android SDK at /Users/asdf/Library/Android/sdk
• Platform android-31, build-tools 31.0.0
• ANDROID_HOME = /Users/asdf/Library/Android/sdk
• ANDROID_SDK_ROOT = /usr/local/share/android-sdk
• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 13.1, Build version 13A1030d
• CocoaPods version 1.11.2
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 4.2)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264)
[✓] VS Code (version 1.61.2)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.27.0
[✓] Connected device (1 available)
• Chrome (web) • chrome • web-javascript • Google Chrome 95.0.4638.69
• No issues found!