简体   繁体   English

无状态小部件列表的常量值无效

[英]Invalid constant value for List of Stateless Widgets

    class HomeScreen extends StatelessWidget {
  const HomeScreen({Key? key}) : super(key: key);

  final List pages = const [
    MessagesPage(),
    NotificationsPage(),
    CallsPage(),
    ContactsPage(),
  ];

  @override
  Widget build(BuildContext context) {
    return const Scaffold(
      body: pages[0],
      bottomNavigationBar: _BottomNavigationBar(),
    );
  }
}

There's an Invalid constant value error in body: pages[0] if I call the Widget directly like body: MessagesPage() then there's no error. body: pages[0]中存在 Invalid constant value 错误,如果我像body: MessagesPage()一样直接调用 Widget,则没有错误。 I tried making everything const but nothing's helping.我尝试将所有内容都设为 const 但没有任何帮助。

Is there any update in the new version of Flutter or have I done some mistake? Flutter新版本有更新还是我做错了什么?

remove const删除const

  @override
  Widget build(BuildContext context) {
    return Scaffold( // here need to change
      body: pages[0],
      bottomNavigationBar: _BottomNavigationBar(),
    );
  }

remove const keyword:删除const关键字:

  class HomeScreen extends StatelessWidget {
  const HomeScreen({Key? key}) : super(key: key);

  final List pages = const [
    MessagesPage(),
    NotificationsPage(),
    CallsPage(),
    ContactsPage(),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(//remove const keyword here
      body: pages[0],
      bottomNavigationBar: _BottomNavigationBar(),
    );
  }
}

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

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