Home > Back-end >  Invalid constant value for List of Stateless Widgets
Invalid constant value for List of Stateless Widgets

Time:12-15

    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. I tried making everything const but nothing’s helping.

Is there any update in the new version of Flutter or have I done some mistake?

CodePudding user response:

remove const keyword:

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

CodePudding user response:

remove const

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