【问题标题】:The parameter 'image' can't have a value of 'null' because of its type, but the implicit default value is 'null'参数 'image' 的值不能为 'null' 因为它的类型,但隐含的默认值是 'null'
【发布时间】:2021-10-28 15:43:52
【问题描述】:

我不理解以下代码的问题,并希望听到一些关于如何解决我的问题的想法

代码:

class SplashContent extends StatelessWidget {
  const SplashContent({
    Key? key,
    this.text,
    this.image,

  }) : super(key: key);
  final String text, image;

【问题讨论】:

  • textimage 上添加required 关键字

标签: flutter constructor null this nullable


【解决方案1】:

当我们在构造函数上使用{} 时,它被称为named paramter/Constructor。默认情况下,所有参数在这里都是可选的(命名构造函数)。虽然我们有 final String text, image; 并且没有分配任何值,但由于null-safety,它会显示错误。

我们需要在 this.image, 之前添加required,例如required this.image,..

如果您希望接受空值,请将其设为可空值,例如 String? file。并确保在使用可为空值时。此外,您可以在构造函数上提供默认值。

【讨论】:

    猜你喜欢
    • 2021-06-24
    • 2021-11-12
    • 2022-01-03
    • 2022-01-20
    • 1970-01-01
    • 2021-09-05
    • 2022-06-14
    • 2022-08-13
    • 2021-10-26
    相关资源
    最近更新 更多