Home > Mobile >  flutter - Field isn't final, but constructor is 'const'
flutter - Field isn't final, but constructor is 'const'

Time:03-20

Hi i am quite new with flutter, i hope someone can help me solve this problem...

Can anyone explain to me how i can fix this error?

i am on flutter 2.10.1

lib/00appunti.dart:86:9: Error: Constructor is marked 'const' so all fields must be final.
  const textField({Key? key, required this.changeClass}) : super(key: key);
        ^
lib/00appunti.dart:88:8: Context: Field isn't final, but constructor is 'const'.
  bool changeClass = false;
       ^

This is my code:


void main() => runApp(mainApp());

class mainApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: chat(),
    );
  }
}

class chat extends StatefulWidget {
  const chat({Key? key}) : super(key: key);

  @override
  _chatState createState() => _chatState();
}

class _chatState extends State<chat> {
  bool changeClass = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: changeClass ? container() : textField(changeClass: changeClass),
    );
  }
}

class textField extends StatefulWidget {
  const textField({Key? key, required this.changeClass}) : super(key: key);

  bool changeClass = false;

  @override
  _textFieldState createState() => _textFieldState();
}

class _textFieldState extends State<textField> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Row(
        children: [
          Container(
            width: 300.0,
            height: 60.0,
            color: Colors.red,
          ),
          RawMaterialButton(
            onPressed: () {
              setState(() {
                widget.changeClass = true;
              });
            },
            child: Icon(Icons.send),
          )
        ],
      ),
    );
  }
}

class container extends StatefulWidget {
  const container({Key? key}) : super(key: key);

  @override
  _containerState createState() => _containerState();
}

class _containerState extends State<container> {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        width: double.infinity,
        height: 60.0,
        color: Colors.grey,
      ),
    );
  }
}


hope someone can help me.

Thank you :) I write this piece because otherwise it won't make me post I write this piece because otherwise it won't make me post I write this piece because otherwise it won't make me post

CodePudding user response:

Just remove the constant keyword from const textField({Key? key, required this.changeClass}) : super(key: key);

  • Related