skip to Main Content

Here is the Widget that is getting the error:
The values in a const list literal must be constants. Try removing the keyword ‘const’ from the list literal.

ResponsiveCenter(
                padding: EdgeInsets.symmetric(
                    horizontal: ResponsiveCenterEdgeInsets.horizontal,
                    vertical: ResponsiveCenterEdgeInsets.vertical),
                child: PrimaryButton(
                    text: 'Weight',
                    isLoading: false,
                    **onPressed: () => context.pushNamed(AppRoute.weightHistory.name))**) // <-this code is throwing the error

Here is the "PrimaryButton" widget:

class PrimaryButton extends StatelessWidget {
  const PrimaryButton(
      {super.key, required this.text, this.isLoading = false, this.onPressed});
  final String text;
  final bool isLoading;
  final VoidCallback? onPressed;

  @override
  Widget build(BuildContext context) {
    return SizedBox(
        child: ElevatedButton(
            style: ElevatedButton.styleFrom(
                backgroundColor: Colors.blue,
                foregroundColor: Colors.white,
                padding: const EdgeInsets.all(2.0)),
            onPressed: onPressed,
            child: isLoading
                ? const CircularProgressIndicator(
                    color: Colors.white,
                  )
                : Text(
                    text,
                    textAlign: TextAlign.center,
                  )));
  }
}

Here is the "AppRouter" widget:

enum AppRoute { home, weightHistory, zoneInfo }

final goRouterProvider = Provider<GoRouter>((ref) {
  return GoRouter(initialLocation: '/', debugLogDiagnostics: true, routes: [
    GoRoute(
      path: '/',
      name: AppRoute.home.name,
      builder: (context, state) => const HomeScreen(),
    ),
    GoRoute(
      path: '/weightHistory',
      name: AppRoute.weightHistory.name,
      builder: (context, state) => const WeightHistoryScreen(),
    ),
    GoRoute(
      path: '/zoneInfo',
      name: AppRoute.zoneInfo.name,
      builder: (context, state) => const ZoneInfoScreen(),
    )
  ]);
});

There is no "const" in the error’d code. What am I missing here?

2

Answers


  1. Chosen as BEST ANSWER

    I realized my problem, the "const" declaration was further up the Widget tree, declaring "Scaffold" const.


  2. You have to remove that const key before your PrimaryButton

    class PrimaryButton extends StatelessWidget {
      PrimaryButton(
          {super.key, required this.text, this.isLoading = false, this.onPressed});
      final String text;
      final bool isLoading;
      final VoidCallback? onPressed;
    
      @override
      Widget build(BuildContext context) {
        return SizedBox(
            child: ElevatedButton(
                style: ElevatedButton.styleFrom(
                    backgroundColor: Colors.blue,
                    foregroundColor: Colors.white,
                    padding: const EdgeInsets.all(2.0)),
                onPressed: onPressed,
                child: isLoading
                    ? const CircularProgressIndicator(
                        color: Colors.white,
                      )
                    : Text(
                        text,
                        textAlign: TextAlign.center,
                      )));
      }
    }
    

    you can replace that with your widget

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search