【发布时间】:2021-08-28 21:31:50
【问题描述】:
问题: 我在使用 GetX 和 AutoRoute 设置导航时遇到问题。
代码设置: 根据 GetX 文档,如果要使用 GetX 导航,则必须将 MaterialApp() 替换为 GetMaterialApp()。您还可以设置路线。
void main() {
runApp(
GetMaterialApp(
initialRoute: '/',
getPages: [
GetPage(name: '/', page: () => MyHomePage()),
GetPage(name: '/second', page: () => Second()),
GetPage(
name: '/third',
page: () => Third(),
transition: Transition.zoom
),
],
)
);
}
AutoRoute 示例使用 MaterialApp.router() 来设置 routerDelegate 和 routeInformationParser。
final _appRouter = AppRouter()
...
Widget build(BuildContext context){
return MaterialApp.router(
routerDelegate: _appRouter.delegate(...initialConfig),
routeInformationParser: _appRouter.defaultRouteParser(),
),
}
这是我根据 Getx 和 AutoRoute 设置导航的方法:
void main() {
configureDependencies();
runApp(Portfolio());
}
class Portfolio extends StatelessWidget {
final _appRouter = AppRouter.Router();
@override
Widget build(BuildContext context) {
return GetMaterialApp.router(
routerDelegate: _appRouter.delegate(),
routeInformationParser: _appRouter.defaultRouteParser(),
builder: (context, extendedNav) => Theme(
data: ComplexReduxTheme.complexReduxLightTheme,
child: extendedNav ?? Container(color: Colors.red),
),
);
}
}
我正在使用返回 GetMaterialApp 的 GetMaterialApp.router。尽管如此,我还是收到错误消息“您正在尝试在没有 GetMaterialApp 或 Get.key 的情况下使用无上下文导航。”。我尝试设置导航键并设置 Get.testMode = true 但是当我尝试导航到另一个屏幕时没有任何反应(没有错误)。
期望的结果: 我应该能够通过 Get.toNamed() 导航到所需的屏幕。
当前结果: 尝试使用 Get.toNamed() 导航到另一个屏幕时,我从 GetX 收到以下错误:“您正在尝试使用无上下文导航而不 GetMaterialApp 或 Get.key。 如果您正在测试您的应用程序,您可以使用: [Get.testMode = true],或者如果您正在运行您的应用程序 物理设备或模拟器,您必须交换您的 [MaterialApp] 对于 [GetMaterialApp]。”
自动路由版本: 2.2.0
获取版本: 4.1.4
【问题讨论】:
标签: flutter flutter-getx