简体   繁体   English

访问 Flutter 中的 Object 数据

[英]Access Object Data in Flutter

I'm new to Flutter and trying to get some basics done.我是 Flutter 的新手,并试图完成一些基础知识。 I would like to access data from an object on several places within my code.我想在我的代码中的几个位置访问来自 object 的数据。

I'm most likely mixing up scope or variable types within oo code.我很可能在 oo 代码中混淆了 scope 或变量类型。 So far I only used basic coding using javascript or php and never played to much with objects and stuff.到目前为止,我只使用了 javascript 或 php 的基本编码,并且从未玩过太多的对象和东西。

Please see my sample code below:请参阅下面的示例代码:

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'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

带有我的意图和错误的屏幕截图

You need to access your Map properly.您需要正确访问您的Map

Instead of pages[0] or pages['Events'] , you want to specify the correct key :您想要指定正确的,而不是pages[0]pages['Events']

pages['events']

Map keys are case-sensitive because they use the equals operator . Map 键区分大小写,因为它们使用等于运算符


You cannot access a Map when creating a const .创建const时,您无法访问Map So remove const at const <BottomNavigationBarItem>[ :所以在const <BottomNavigationBarItem>[删除const

<BottomNavigationBarItem>[
  ...
]

Additionally, for the icon you need to use pages['events']['icon'] .此外,对于您需要使用pages['events']['icon']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM