I am getting an error for creating a Column Widget saying 'The constructor being called isn't a const
constructor.' Having a tough time creating a Column Widget itself,
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'My First Flutter App',
theme: ThemeData(
scaffoldBackgroundColor: Colors.white,
),
home: const WelcomeScreen());
}
}
class WelcomeScreen extends StatelessWidget {
const WelcomeScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Column(
children: <Widget>[
Text("Login"),
]
)
);
}}
CodePudding user response:
Just remove const
from Scaffold
widget
CodePudding user response:
Column
is a non-const constructor
Column({
Key? key,
You can use const
where everything is available as compile time constant. In this case, Column
breaks the condition. You can use const
before children, it is possible with Text
for now
class WelcomeScreen extends StatelessWidget {
const WelcomeScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(children: const <Widget>[
Text("Login"),
]),
);
}
}
I will encourage you to lean more about const
, you can check this question and on guides.
CodePudding user response:
The error is at the level of the scaffold widget.
Just remove the const before scaffold. Then add const keyword before the children of your column widget.
That is how your code is supposed to be
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'My First Flutter App',
theme: ThemeData(
scaffoldBackgroundColor: Colors.white,
),
home: const WelcomeScreen());
}
}
class WelcomeScreen extends StatelessWidget {
const WelcomeScreen({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: const <Widget>[
Text("Login"),
...
]
)
);
}}
CodePudding user response:
Remove the const
from the Scaffold
widget and it would work fine. But there will be another warning telling that the Column
widget does not have const
.
To fix that you can either put const
in front of <Widget>
or put const
in front of the children of columns