创建 Flutter 类 和小部件时出现 null 安全错误

null safety error when creating Flutter classes and widgets

我正在学习 Flutter / Dart,在做这个练习时 video 我遇到了一个错误,据我所知,这是由于 null-safety 功能造成的,因为示例来自以前的版本,问题就出现了。

import 'package:flutter/material.dart';

class OurImage extends StatelessWidget {
  final String pathImage;
  final double widthImage;
  final double heightImage;
  OurImage({this.pathImage, this.heightImage, this.widthImage});

  @override
  Widget build(BuildContext context) {
    final photo = Container(
      width: this.widthImage,
      height: this.heightImage,
      margin: EdgeInsets.only(right: 20.0),
      decoration: BoxDecoration(
          image: DecorationImage(
              image: AssetImage(this.pathImage), fit: BoxFit.cover)),
    );

    return photo;
  }
}

The parameter 'pathImage' can't have a value of 'null' because of its type, but the implicit default value is 'null'. Try adding either an explicit non-'null' default value or the 'required' modifier.

在阅读这些关于空安全性的错误 , , 时,我考虑通过添加“?”来更正出现在我面前的错误。和 ”!”到我的代码,我用它验证错误不再出现。

import 'package:flutter/material.dart';

class OurImage extends StatelessWidget {
  final String? pathImage; //change here
  final double? widthImage; //change here
  final double? heightImage; //change here
  OurImage({this.pathImage, this.heightImage, this.widthImage});

  @override
  Widget build(BuildContext context) {
    final photo = Container(
      width: this.widthImage,
      height: this.heightImage,
      margin: EdgeInsets.only(right: 20.0),
      decoration: BoxDecoration(
          image: DecorationImage(
              image: AssetImage(this.pathImage!), fit: BoxFit.cover)), //change here
    );

    return photo;
  }
}

我的修复方法对吗?我应该如何创建 OurImage class 的变量,以便它不会生成此错误?

你可以让任何东西都可以为空,但你应该在你的整个代码和每个使用该项目的地方考虑它。您需要为值为 null 时提供可替换的行为(因为由于可空性而可能)

例如,对于可为 null 的属性,您应该提供一个默认值,如 (1) 或在 (2)

等用法中处理它

(1)

import 'package:flutter/material.dart';

class OurImage extends StatelessWidget {
  final String? pathImage; //change here
  final double? widthImage; //change here
  final double? heightImage; //change here
  OurImage({this.pathImage = 'default image path', this.heightImage = 100, this.widthImage = 100});

  @override
  Widget build(BuildContext context) {
    final photo = Container(
      width: this.widthImage,
      height: this.heightImage,
      margin: EdgeInsets.only(right: 20.0),
      decoration: BoxDecoration(
          image: DecorationImage(
              image: AssetImage(this.pathImage), fit: BoxFit.cover)), //change here
    );

    return photo;
  }
}

(2)

import 'package:flutter/material.dart';

class OurImage extends StatelessWidget {
  final String? pathImage; //change here
  final double? widthImage; //change here
  final double? heightImage; //change here
  OurImage({this.pathImage, this.heightImage, this.widthImage});

  @override
  Widget build(BuildContext context) {
    final photo = Container(
      width: this.widthImage ?? 100,
      height: this.heightImage ?? 100,
      margin: EdgeInsets.only(right: 20.0),
      decoration: BoxDecoration(
          image: DecorationImage(
              image: AssetImage(this.pathImage ?? 'default image path'), fit: BoxFit.cover)), //change here
    );

    return photo;
  }
}