0

i tried to pass a State object to Counter widget as a child, I get the following error:-

Error compiling to JavaScript: /tmp/dartpadWVXESU/lib/main.dart:59:18: Error: Cannot invoke a non-'const' constructor where a const expression is expected. Try using a constructor or factory that is 'const'. child: _CounterState(),

can anyone please explain me what is happening and why.

void main() {
  runApp(
    const MaterialApp(
      home: Scaffold(
        body: Center(
          child: _CounterState(),
        ),
      ),
    ),
  );
}

_CounterState is a class extending State class

1 Answers1

0

Your _CounterState() is probably not a StatefulWidget nor a StatelessWidget.

Perhaps your _CounterState() definition looks like :

class _CounterState extends State<Counter> {
...
}

You can't pass any type of class to a child of a built in Widget in Flutter.

You can only pass in the instance of a class which extends or implements a Widget class.

Sushan Shakya
  • 331
  • 1
  • 6