Reputation: 1
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.
Upvotes: 0
Views: 518
Reputation: 1
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'),
),
),
),
);
}
Upvotes: 0
Reputation: 61
To be a bit more precise: AppBar doesn't have a const constructor, so you can't use the const keyword. However, Text does have a const constructor, so you can (should) use const for that one.
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('I am Rich'),
),
),
),
);
}
Upvotes: 1
Reputation: 25
Just remove const
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('I am Rich'),
),
),
),
);
}
Upvotes: 0