【问题标题】:Can't map a List of items (Objects reference) in dart无法在飞镖中映射项目列表(对象引用)
【发布时间】:2021-05-12 13:43:01
【问题描述】:

我遇到了地图循环问题,我无法映射我的项目列表 也许这是因为是参考列表?有什么解决方案可以改变这样做的形状吗?

我的代码:

NavigationRoute initialRoute = NavigationRoute(
  route: '/',
  widget: Home(),
);

NavigationRoute home = NavigationRoute(
  title: 'Home',
  route: '/home',
  icon: Icons.home,
  color: Colors.teal,
  widget: Home(),
);

NavigationRoute cardsNavigator = NavigationRoute(
  title: 'Cards',
  route: '/cardsNavigator',
  icon: Icons.card_giftcard,
  color: Colors.teal,
  widget: CardsNavigator(),
);

NavigationRoute example = NavigationRoute(
  title: 'Example',
  route: '/example',
  icon: Icons.business,
  color: Colors.teal,
  widget: ExamplePage(),
);

List<NavigationRoute> navigation = <NavigationRoute>[
  initialRoute,
  home,
  cardsNavigator,
  example,
];

我试图在项目地图中使用它:

  bottomNavigationBar: BottomNavigationBar(
            currentIndex: currentIndex,
            onTap: (index) {
              setState(() {
                currentIndex = index;
                digitalBankingNavigator.currentState?.pushNamed(
                    digitalBankingRoutes.navigation[currentIndex].route);
              });
            },
            items: digitalBankingRoutes.navigation
                .map((route) => BottomNavigationBarItem(
                    icon: Icon(route.icon),
                    label: route.title,
                    backgroundColor: route.color))
                .toList(),
          ),

【问题讨论】:

  • 你能分享一下显示错误吗?
  • @MiguelEscobarCalderon 没有显示错误,我认为这是因为这是一个引用列表(因为当我调试时,我无法访问对象)

标签: flutter dart


【解决方案1】:

initialRoute 中,titlenull,但BottomNavigationBarItem 中的label 字段不能是null

所以,你必须在initialRoute 中给出一些标题。

NavigationRoute initialRoute = NavigationRoute(
  // Provided a title here
  title: 'Some Title'
  route: '/',
  widget: Home(),
);

NavigationRoute home = NavigationRoute(
  title: 'Home',
  route: '/home',
  icon: Icons.home,
  color: Colors.teal,
  widget: Home(),
);

NavigationRoute cardsNavigator = NavigationRoute(
  title: 'Cards',
  route: '/cardsNavigator',
  icon: Icons.card_giftcard,
  color: Colors.teal,
  widget: CardsNavigator(),
);

NavigationRoute example = NavigationRoute(
  title: 'Example',
  route: '/example',
  icon: Icons.business,
  color: Colors.teal,
  widget: ExamplePage(),
);

List<NavigationRoute> navigation = <NavigationRoute>[
  initialRoute,
  home,
  cardsNavigator,
  example,
];

如果您有任何疑问,请发表评论。

【讨论】:

  • 您好!感谢您的评论,这不是错误,我在发布问题之前尝试过。无论如何,我决定将我的变量结构更改为某个时候我可以从其他页面访问的内容,并在需要的每个导航中列出地图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-06
  • 2020-02-11
  • 2018-10-15
  • 2020-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多