简体   繁体   English

被调用的构造函数不是 const 构造函数

[英]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 ,你可以检查这个问题指南

The error is at the level of the scaffold widget.错误在脚手架小部件级别。

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.

相关问题 Flutter 错误:被调用的构造函数不是 const 构造函数 - Flutter error : The constructor being called isn't a const constructor 被调用的构造函数不是 const 构造函数。 尝试从构造函数调用中删除“const”。 Flutter怎么解决? - The constructor being called isn't a const constructor. Try removing 'const' from the constructor invocation. Flutter How to solve? Flutter 错误 - 如何解决:被调用的构造函数不是 const 构造函数。 尝试从构造函数调用中删除“const” - Flutter Error - how to solve: The constructor being called isn't a const constructor. Try removing 'const' from the constructor invocation flutter const 构造函数错误 - flutter const constructor error 错误:找不到构造函数“ImagePickerOptions”。 ImagePickerOptions 选项 = const ImagePickerOptions() - Error: Couldn't find constructor 'ImagePickerOptions'. ImagePickerOptions options = const ImagePickerOptions() const 构造函数和 no_const 构造函数有什么区别? - What's the difference between const constructor and no_const constructor? Dart:使用const构造函数有不利之处吗? - Dart: Is there a disadvantage to using const constructor? Dart const 构造函数 - 它可以是默认的吗? - Dart const constructor - could it be default? 在构造函数中调用SetState() - SetState() called in constructor 为什么在flutter中使用provider时不执行类的构造函数? - Why the constructor of class isn't executed when using provider in flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM