Home > Blockchain >  Invalid constant value in extended class
Invalid constant value in extended class

Time:02-11

The following code snippet displays some styled TextFields in a flutter app:

@override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: const Text('Some input form')),
        body: Container(
          padding: const EdgeInsets.all(32),
            child: Column(
          children: const [
            Placeholder(fallbackHeight: 200, fallbackWidth: 200),
            SizedBox(height: 20),
            TextField(decoration: InputDecoration(labelText: 'Label0')),
            SizedBox(height: 20),
            TextField(decoration: InputDecoration(labelText: 'Label1')),
          ],
        )));
  }

Because I want all my TextFields to look the same I thought of doing this:

class mTextField extends TextField{
  const mTextField({Key? key, String? hint}) : super(key: key, decoration: InputDecoration(labelText: hint));//this line gives the error
}

To remodel my original code as

@override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: const Text('Some input form')),
        body: Container(
          padding: const EdgeInsets.all(32),
            child: Column(
          children: const [
            Placeholder(fallbackHeight: 200, fallbackWidth: 200),
            SizedBox(height: 20),
            mTextField(hint: 'Label0'),//simplified here
            SizedBox(height: 20),
            mTextField(hint: 'Label1'),//simplified here
          ],
        )));
  }

But this gives me an "invalid constant value" error for the labelText: hint. Of course I could just make the children of the Container non-const, but as far as I see it, it should be possible to have mTextField be constant because it is no different from a regular TextField which can be const.

CodePudding user response:

Change

class mTextField extends TextField{
  const mTextField({Key? key, String? hint}) : super(key: key, decoration: InputDecoration(labelText: hint));//this line gives the error
}

to

class mTextField extends TextField{
  mTextField({Key? key, String? hint}) : super(key: key, decoration: InputDecoration(labelText: hint));//this line gives the error
}

CodePudding user response:

The const giving the error is the const in the mTextfield not the Column children. You can remove the const in the mTextField

class mTextField extends TextField{
 mTextField({Key? key, String? hint}) : super(key: key, decoration: InputDecoration(labelText: hint));//this line gives the error
}

Although this will cause the Column const to give the error. You can convert your mTextfield to be built like this instead

        class mTextField extends StatelessWidget{
     const mTextField({Key? key, this.hint}) : super(key: key);
      
      final String? hint;
      
      @override
      Widget build(BuildContext context){
        return TextField(decoration: InputDecoration(labelText: hint));
      }
    }
  • Related