【问题标题】:The values in a const list literal must be constants, I am getting this error while adding an Image to Column Widget in Flutterconst 列表文字中的值必须是常量,在 Flutter 中将图像添加到列小部件时出现此错误
【发布时间】:2022-01-03 13:26:36
【问题描述】:

为什么我无法在此处添加图片?在 Flutter 中将图像添加到列小部件时出现此错误。当我删除 new 关键字时,我收到另一个错误。请检查这个问题。我创建了一个名为 assets>images>girl-icon.jpg 的文件夹。另外,我启用了 pubspec.yaml 文件中的资产。

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(
      appBar: AppBar(
        title: const Text('First Page'),
        backgroundColor: Colors.amber,
      ),
      body:Center(

      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: const <Widget> [

          Text("Login",
              style: TextStyle(
              fontSize: 30,
              fontWeight: FontWeight.bold,
              )),
          new Image.asset("assets/images/girl-icon.jpg"),
        ],
        ),
        heightFactor: 30,

      ),
      );
    }}

See screenshot of the problem here

【问题讨论】:

  • 尝试从图片中删除新关键字
  • 执行此操作时出现另一个错误。我收到此错误The constructor being called isn't a const constructor.
  • 删除 `children: const [all you widgets]`中的 const `
  • const &lt;Widget&gt;中删除常量
  • 它工作正常,谢谢! @RavindraS.Patil

标签: flutter flutter-layout


【解决方案1】:

const 需要硬编码的 constant 值,并且您在构建 dynamically 时将 MyApp 称为常量,这就是它抛出错误的原因。在 MyApp 之前删除 const 以解决此问题:

void main() {
  runApp(MyApp());
}

还有从这里:

children: <Widget> [

          Text("Login",
              style: TextStyle(
              fontSize: 30,
              fontWeight: FontWeight.bold,
              )),
          new Image.asset("assets/images/girl-icon.jpg"),
        ]

【讨论】:

    【解决方案2】:
    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(
          appBar: AppBar(
            title: const Text('First Page'),
            backgroundColor: Colors.amber,
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text("Login",
                    style: TextStyle(
                      fontSize: 30,
                      fontWeight: FontWeight.bold,
                    )),
                Image.asset("assets/images/girl-icon.jpg"),
              ],
            ),
            heightFactor: 30,
          ),
        );
      }
    }
    

    我的方法就是删除

    const <Widget> 
    

    【讨论】:

    • 谢谢,它正在工作!
    • 我的代码在哪里 ;)
    • 你想说什么?
    猜你喜欢
    • 2021-11-30
    • 2021-06-29
    • 2020-04-29
    • 2021-08-16
    • 2020-01-06
    • 1970-01-01
    • 2016-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多