Home > Enterprise >  Flutter Scaffold(appBar: AppBar()) not working 'const' error
Flutter Scaffold(appBar: AppBar()) not working 'const' error

Time:02-05

void main() {
  runApp(
    const MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('I am Rich'),
        ),
      ),
    ),
  );
}

this is my code and there is showing "the constructor called is not a constructor"

Error message-

lib/main.dart:8:17: Error: Cannot invoke a non-'const' constructor where a const expression is expected.
Try using a constructor or factory that is 'const'.
        appBar: AppBar(
                ^^^^^^

tried adding 'const' before scaffold

I am sorry for the silly question. I am just a beginner. can't find the solution anywhere else.

CodePudding user response:

Remove the 'const' before MaterialApp. It should work fine.

the correct code should be - void main() { runApp( MaterialApp( home: Scaffold( appBar: AppBar( title: Text('I am Rich'), ), ), ), ); }

CodePudding user response:

Just remove const

void main() {
  runApp(
    MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('I am Rich'),
        ),
      ),
    ),
  );
}
  • Related