gpt4 book ai didi

flutter - 在 Flutter 中访问对象数据

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

我是 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/

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