Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Widget.Variable Does not work to access string variable form stful constructor

I build a constructor in stateful class that take one string parameter, then when I call this parameter using widget.label, it gives me this error

"Invalid constant value."

here is my code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import 'package:flutter/material.dart';

class TextFieldDecoration extends StatefulWidget {
  final String? label;
  const TextFieldDecoration({Key? key, this.label}) : super(key: key);
  @override
  _TextFieldDecorationState createState() => _TextFieldDecorationState();
}

class _TextFieldDecorationState extends State<TextFieldDecoration> {
  @override
  Widget build(BuildContext context) {
    return const TextField(
      decoration: InputDecoration(
        labelStyle: TextStyle(color: Colors.black45),
        labelText: widget.label,
      ),
      style: TextStyle(
        color:  Color(0xff1f8ac0),
      ),
    );
  }
}

enter image description here

>Solution :

The error occurs because you defined your TextField as constant but you variable cannot be constant. To fix your issue, just remove const in front of TextField.

class _TextFieldDecorationState extends State<TextFieldDecoration> {
  @override
  Widget build(BuildContext context) {
    return TextField(
      decoration: InputDecoration(
        labelStyle: TextStyle(color: Colors.black45),
        labelText: widget.label,
      ),
      style: TextStyle(
        color:  Color(0xff1f8ac0),
      ),
    );
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading