flutter 无法定义“const”构造函数,因为字段“mylist”是用非常量值初始化的

cbwuti44  于 2023-01-02  发布在  Flutter
关注(0)|答案(2)|浏览(364)

我发现了一个问题,因为我刚刚学了最新的flutter,而我学的是flutter的旧版本。
当我把myList变量和Children分开时
错误:

Can't define the 'const' constructor because the field 'mylist' is initialized with a non-constant value.
Try initializing the field to a constant value, or removing the keyword 'const' from the constructor. [Ln 8, Col 3]
Can't define a const constructor for a class with non-final fields.
Try making all of the fields final, or removing the keyword 'const' from the constructor. [Ln 9, Col 9]

脚本:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  List<Widget> mylist = [
            Container(
              height: 300,
              width: 300,
              color: Colors.red,
            ),
            Container(
              height: 300,
              width: 300,
              color: Colors.green,
            ),
            Container(
              height: 300,
              width: 300,
              color: Colors.blue,
            ),
            Container(
              height: 300,
              width: 300,
              color: Colors.amber,
            ),
          ];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("List View"),
        ),        
        body: ListView(
          //scrollDirection: Axis.horizontal,
          children: mylist,
        ),
      ),
    );
  }
}

请帮助和解释的问题和参考资料的细节

bvk5enib

bvk5enib1#

删除const关键字,因为MyApp()不是const蚂蚁:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  MyApp({super.key});
  List<Widget> mylist = [
    Container(
      height: 300,
      width: 300,
      color: Colors.red,
    ),
    Container(
      height: 300,
      width: 300,
      color: Colors.green,
    ),
    Container(
      height: 300,
      width: 300,
      color: Colors.blue,
    ),
    Container(
      height: 300,
      width: 300,
      color: Colors.amber,
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("List View"),
        ),
        body: ListView(
          //scrollDirection: Axis.horizontal,
          children: mylist,
        ),
      ),
    );
  }
}

有关说明,请参见:What is the difference between the "const" and "final" keywords in Dart?
如果你的值是在运行时计算出来的(例如new DateTime.now()),你就不能用常量来表示它。但是,如果这个值在编译时是已知的(const a = 1;),那么你应该使用const而不是final。const和final还有另外两个很大的区别。首先,如果你在一个类中使用const,你必须把它声明为静态const而不仅仅是const。其次,如果你有一个const集合,里面的所有东西都在const中。如果你有一个final集合,里面的所有东西都不是final。

ecbunoof

ecbunoof2#

因为mylist是可变的,这意味着它可以被改变,可以被重新赋值,包含它的类不能再是常量了。
const类是其成员也是const(常数)的类。
因此,如果您希望在代码中重新赋值mylist值,则需要从类构造函数中删除const关键字,如下所示:

class MyApp extends StatelessWidget {
  MyApp({super.key}); // removed const
    List<Widget> mylist = [
 // ...

相关问题