skip to Main Content

Error 1:

Invalid constant value.

Error 2:

The values in a const list literal must be constants.
Try removing the keyword ‘const’ from the list literal.

These errors appear in this code (on the line with Navigator.pushNamed...) :

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:my_app/global/common/toast.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text("SALARY CALCULATOR"),
          centerTitle: true,
        ),
        drawer: const Drawer(
          backgroundColor: Color.fromARGB(255, 22, 116, 192),
          child: Column(
            children: [
              DrawerHeader(
                child: Icon(
                  Icons.wallet,
                  color: Colors.white,
                  size: 45,
                ),
              ),
              ListTile(
                leading: Icon(Icons.settings),
                title: Text('S E T T I N G S'),
                onTap: () {
                  Navigator.pushNamed(context, '/settingspage');
                }, 
              )
            ],
          ),
        ),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const SizedBox(height: 30),
            GestureDetector(
              onTap: () {
                FirebaseAuth.instance.signOut();
                Navigator.pushNamed(context, "/login");
                showToast(message: "Successfully signed out");
              },
              child: Container(
                height: 45,
                width: 100,
                decoration: BoxDecoration(
                  color: Colors.blue,
                  borderRadius: BorderRadius.circular(10),
                ),
                child: const Center(
                  child: Text(
                    "Sign out",
                    style: TextStyle(
                      color: Colors.white,
                      fontWeight: FontWeight.bold,
                      fontSize: 18,
                    ),
                  ),
                ),
              ),
            )
          ],
        ));
  }
}


At the moment for the application I managed to finish the authentication part with the help of firebase, with register and login, after you create your account it directs you to home_page.dart where there is an AppBar at the top with the name of the application, I also created a navigation drawer where if you press ,a window opens where I have the application logo + the settings page and the profile page, the problem is that when I click on SETTINGS it does not direct me to the respective page

a picture with a part of the code and all the files and folders in the left

and this one is with code from main.dart but I don’t know what to change to get rid of that error/problem

2

Answers


  1. On line 20 you created a Drawer with its constant constructor, so the child has to be constant too. So the Column is also constructed with its constant constructor, and now its children have to be constants. Unfortunately, ListTile can’t be constant because a callback is passed to its onTap parameter, and that callback is not a constant. So the solution is to remove const from the Drawer constructor in line 20.

    drawer: Drawer(
    

    You can keep the DrawerHeader constant though, so on line 24 put the const keyword before the constructor:

    const DrawerHeader(
    
    Login or Signup to reply.
  2. in your code you made Drawer a const and its child is column and one of its children is a listtile. the listtile`s property ontap will work only if its parents does not have any const. so by removing const from drawer will solve your issue.

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