How to solve: Invalid constant value for List of Stateless Widgets

Question

Asked by Prerak on December 15, 2021 (source).

    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?

Answer

Question answered by Ravindra S (source).

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(),
    );
  }
}
ANDROID-STUDIO DART FLUTTER
SHARE: