gpt4 book ai didi

flutter - 如何从路线类别列表中生成路线图

转载 作者:行者123 更新时间:2023-12-03 04:55:17 25 4
gpt4 key购买 nike

我想通过创建路由列表来自动在我的应用程序中分配路由。
然后将映射此列表。我的目的是改进现有代码。
我知道这令人困惑,您可以通过以下代码更好地理解它:

MaterialApp(
navigatorObservers: <NavigatorObserver>[observer],
home: getWidgetByRouteName(hasRoute), //GameDetails(),
routes: <String, WidgetBuilder>{
SplashPage.routeName: (BuildContext context) => SplashPage(),
Phone.routeName: (BuildContext context) => Phone(),
OTP.routeName: (BuildContext context) => OTP(),
Team.routeName: (BuildContext context) => Team(),

},
),

我想将上面的代码转换成这种类型:

List route = [SplashPage, Phone, OTP];

Map<String, WidgetBuilder> namedRoutes = {};

route.forEach((element) {
namedRoutes[element.routeName] = (BuildContext context) => element();
});



MaterialApp(
navigatorObservers: <NavigatorObserver>[observer],
home: getWidgetByRouteName(hasRoute), //GameDetails(),
routes: namedRoutes
),


并且如果您感到困惑,什么是routeName:
class SplashPage extends StatelessWidget {
static const routeName = '/splash';
..
..
..
}

我收到此错误:
The following NoSuchMethodError was thrown building MainApp(dirty):
Class '_Type' has no instance getter 'routeName'.
Receiver: SplashPage
Tried calling: routeName

最佳答案

您不能从表示类的Type对象创建类的实例,至少不能没有镜像(Flutter和Web平台均不支持镜像)。

关于flutter - 如何从路线类别列表中生成路线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60742484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com