Flutter错误:错误:无法在字段初始值设定项中访问“ this”以读取“ computeMaxScale”

时间:2018-08-23 23:00:13

标签: dart flutter

运行flutter upgrade后,我遇到了一些颤振问题

我有一个存储minScalemaxScale的类。

在构造函数中,我想声明它们之间的间隔。由于这些值可以是双精度值或枚举的实例,因此我必须在比较之前对其进行计算。

构造函数的代码并声明:

class ScaleBoundaries {
  final dynamic _minScale;
  final dynamic _maxScale;
  Size size;
  ImageInfo imageInfo;

  ScaleBoundaries(this._minScale, this._maxScale, { @required this.size, @required this.imageInfo}) :
        assert(_minScale is double || _minScale is PhotoViewScaleBoundary),
        assert(_maxScale is double || _maxScale is PhotoViewScaleBoundary),
        assert(computeMinScale() <= computeMaxScale());

这给了我以下错误:

compiler message: file:///.../photo_view/lib/photo_view_scale_boundaries.dart:14:37: Error: Can't access 'this' in a field initializer to read 'computeMaxScale'.
compiler message:         assert(computeMinScale() <= computeMaxScale());
compiler message:                                     ^^^^^^^^^^^^^^^
compiler message: file:///.../photo_view/lib/photo_view_scale_boundaries.dart:14:16: Error: Can't access 'this' in a field initializer to read 'computeMinScale'.
compiler message:         assert(computeMinScale() <= computeMaxScale());
compiler message:                ^^^^^^^^^^^^^^^

那是我扑扑的医生的输出:

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel master, v0.7.1-pre.15, on Linux, locale en_US.UTF-8)
[!] Android toolchain - develop for Android devices (Android SDK 28.0.1)
    ✗ Android license status unknown.
[✓] Android Studio (version 3.1)
[✓] IntelliJ IDEA Community Edition (version 2018.1)
[!] VS Code (version 1.25.1)
[✓] Connected devices (1 available)

! Doctor found issues in 2 categories.

我发现的唯一解决方案是删除断言,但这不行。我该怎么解决?

有关更多信息,请参见整个文件和项目:photo_view_scale_boundaries.dart

1 个答案:

答案 0 :(得分:5)

您可以在构造函数的主体中执行该断言

class Foo {
  Foo() {
    assert(computeMinScale() <= computeMaxScale());
  }    
}