skip to Main Content

Couldn’t find headerLabel to use it. Tried to use variable headerLabel but could not find solution

class ProfileHeaderLabel extends StatelessWidget {
  final String headerLabel;
  const ProfileHeaderLabel({
   Key? key, required this.headerLabel
  }): super(key: key)

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 40,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: const [
          SizedBox(
            height: 40,
            width: 50,
            child: Divider(
              color: Colors.grey,
              thickness: 1,
            ),
          ),
          // 'Account info'
          Text(
            headerLabel,
            style: TextStyle(
                color: Colors.grey, fontSize: 24, fontWeight: FontWeight.w600),
          ),

Couldn’t find headerLabel to use it.

3

Answers


  1. Chosen as BEST ANSWER

    Thank you @MendelG like the way you put it above works, also putting const before Sizedbox

    not before [

    Like this:

    mainAxisAlignment: MainAxisAlignment.center,
            children: [
              const SizedBox(
    

  2. In your code:

    Widget build(BuildContext context) {
        return SizedBox(
            height: 40,
            child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: const [ // --> This line
            SizedBox(
    

    you’re trying to use a non-constant expression (headerLabel) inside a constant (const [...]).

    But in your case, headerLabel is not a constant because its value is determined at runtime, not at compile-time.

    To solve the issue, just remove const from the Row’s children:

     Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [ // Remove `const`
            SizedBox(
    

    Related: (to learn more about const)

    Login or Signup to reply.
    1. You need to add semicolon at the end of your constructor

      const ProfileHeaderLabel({
      Key? key, required this.headerLabel
      }): super(key: key);

    2. You need to remove const keyword after Row children

    You’re trying to access the non-const variable (headerLabel) that’s why it is throwing the error.

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