[英]The constructor being called isn't a const constructor
I am getting an error for creating a Column Widget saying 'The constructor being called isn't a const
constructor.'我在创建 Column Widget 时遇到错误,提示“被调用的构造函数不是
const
构造函数。” 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"),
]
)
);
}}
Just remove const
from Scaffold
widget只需从
Scaffold
小部件中删除const
Column
is a non-const constructor Column
是一个非常量构造函数
Column({
Key? key,
You can use const
where everything is available as compile time constant.您可以使用
const
,其中所有内容都可用作编译时间常数。 In this case, Column
breaks the condition.在这种情况下,
Column
打破了条件。 You can use const
before children, it is possible with Text
for now您可以在孩子之前使用
const
,现在可以使用Text
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 .我会鼓励你更多地了解
const
,你可以检查这个问题和指南。
Just remove the const before scaffold.只需在脚手架之前删除const即可。 Then add const keyword before the children of your column widget.
然后在列小部件的子级之前添加const关键字。
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"), ... ] ) ); }}
Remove the const
from the Scaffold
widget and it would work fine.从
Scaffold
小部件中删除const
,它会正常工作。 But there will be another warning telling that the Column
widget does not have const
.但是会有另一个警告告诉
Column
小部件没有const
。
To fix that you can either put const
in front of <Widget>
or put const
in front of the children of columns
要解决此问题,您可以将
const
放在<Widget>
前面或将const
放在children of columns
前面
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.