flutter - 在 Flutter 中访问对象数据

标签 flutter dart

我是 Flutter 的新手,正在尝试完成一些基础知识。我想从代码中多个位置的对象访问数据。

我很可能在 oo 代码中混淆了作用域或变量类型。到目前为止,我只使用 javascript 或 php 使用了基本的编码,并且从未玩过太多对象和东西。

请参阅下面的示例代码:

class _APPS extends State<APP>
    with SingleTickerProviderStateMixin {

  final pages = {
    "events": {
      "title": "Events",
      "icon": Icon(Icons.event),
      "page": EventsSite(),
    },
    "news": {
      "title": "News",
      "icon": Icon(Icons.message),
      "page": NewsSite(),
    },
    "favorites": {
      "title": "Favorites",
      "icon": Icon(Icons.favorite),
      "page": EventsSite(),
    },
    "profile": {
      "title": "Profile",
      "icon": Icon(Icons.account_circle),
      "page": EventsSite(),
    },
  };

  Widget build(BuildContext context) {
    return MaterialApp(
      home: Builder(
        builder: (context) => Scaffold(
          body: PageView(
            controller: _pageController,
            //physics: NeverScrollableScrollPhysics(),
            onPageChanged: _onPageChanged,
            children: [
              pages[0]['page'], <=== Here is what i'm trying to do
              NewsSite(),
              Icon(Icons.directions_bike),
            ],
          ),
          bottomNavigationBar: BottomNavigationBar(
            type: BottomNavigationBarType.fixed,
            showUnselectedLabels: false,
            backgroundColor: _colorBackgroundBright,
            unselectedItemColor: Colors.white,
            selectedItemColor: _colorHighlight,
            selectedLabelStyle: TextStyle(fontSize: 10.0),
            iconSize: 20,
            currentIndex: _selectedIndex,
            onTap: _onItemTapped,
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(pages['Events']['icon']), <=== Here is what i'm trying to do
                title: Text(pages[0]['title']), <=== Here is what i'm trying to do
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.message),
                title: Text('News'),
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.favorite),
                title: Text('Favorites'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Screenshot with my intent and the error

最佳答案

您需要访问您的 Map适本地。

而不是 pages[0]pages['Events'] ,您要指定正确的 key :

pages['events']

映射键区分大小写,因为它们使用 equals operator .

您无法访问 Map创建 const 时.所以删除constconst <BottomNavigationBarItem>[ :

<BottomNavigationBarItem>[
  ...
]

此外,对于图标,您需要使用 pages['events']['icon'] .

关于flutter - 在 Flutter 中访问对象数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58157707/

相关文章:

android - 如何在 Dart/Flutter 中泛化可选参数

android - flutter 力释放前景

flutter - 如何在Flutter中调用来自不同类的变量

Flutter 可折叠/可扩展卡片

flutter - 我应该如何区分 Flutter 移动版和网页版应用程序?

dart - 如何在 Flutter 的 SliverAppBar 中添加 Tabbar?

flutter - 为什么我的图标的一半在定位小部件中消失了?

dart - 是否可以通过单独构建Polymer.dart元素?

Flutter:自定义字体

android-studio - IDE 没有在 flutter 中显示小部件结束注释