我在下面的代码中遇到了问题,在那里我使用了一个简单的动画Flutter 包.在此声明中发生的问题opacity: animation?["opacity"].它提示我添加空安全,我就这样做了,但随后它给出了这个错误The Operator '[]' isn't defined for the type 'Widget' . Try defining the operator '[]'.如何解决? **MovieTweet代码为**

final tween = MovieTween();
tween.tween('opacity', Tween(begin: 0.0, end: 1.0),
duration: const Duration(milliseconds: 500));

tween.tween('translateY', Tween(begin: 0.0, end: 1.0),
    duration: const Duration(milliseconds: 500),
curve: Curves.easeOut);
return PlayAnimationBuilder<Movie>(
        delay: Duration(milliseconds: (500 * delay).round()),
        tween: tween, // provide tween
        duration: tween.duration,  // total duration obtained from MovieTween
        builder: (context, value, animation) {
          return Opacity(
              opacity: animation?["opacity"],
            child: Transform.translate(
                    offset: Offset(0, animation["translateY"]),
                    child: child
                  ),
          );
        },
    );

我在下面添加code of MovieTween class:

  /// Animates a property and returns the implicitly created scene.
  /// The scene can be used to add further properties to the scene or to
  /// add further scenes to the movie.
  MovieScene thenTween<T>(
    /// Property to animate
    MovieTweenPropertyType property,

    //// Tween that describes the property animation.
    Animatable<T> tween, {

    /// Duration of the scene
    required Duration duration,

    /// Fine-tune the begin time of the next scene by adding a delay.
    /// The value can also be negative.
    Duration delay = Duration.zero,

    /// Custom curve for this property.
    Curve? curve,
    Curve? sceneCurve,
  }) {
    return thenFor(duration: duration, delay: delay, curve: sceneCurve)
        .tween(property, tween, curve: curve);
  }

  /// Adds an additional scene that begins immediately after this scene.
  MovieScene thenFor({
    /// Duration of the scene
    required Duration duration,

    /// Fine-tune the begin time of the next scene by adding a delay.
    /// The value can also be negative.
    Duration delay = Duration.zero,

    /// Custom curve for this scene.
    Curve? curve,
  }) {
    return parent.scene(
      begin: begin + this.duration + delay,
      duration: duration,
      curve: curve,
    );
  }
}

class _SceneItem {
  final MovieTweenPropertyType property;
  final Animatable tween;
  final Curve? curve;
  final Duration shiftBegin;
  final Duration shiftEnd;

  _SceneItem({
    required this.property,
    required this.tween,
    required this.shiftBegin,
    required this.shiftEnd,
    this.curve,
  });
}

/// A snapshot of properties that are animated by a [MovieTween].
/// This class can obtained by using [MovieTween.transform].
class Movie {
  final Map<MovieTweenPropertyType, dynamic> _map;

  Movie({required Map<MovieTweenPropertyType, dynamic> map}) : _map = map;

  /// Returns the value for a given [property].
  V get<V>(MovieTweenPropertyType property) {
    assert(_map.containsKey(property), 'Property $property was not found.');
    return _map[property] as V;
  }
}

class _AbsoluteSceneItem {
  final MovieTweenPropertyType property;
  final Animatable<dynamic> tween;
  final Curve curve;
  final int begin;
  final int end;

  _AbsoluteSceneItem({
    required this.property,
    required this.tween,
    required this.curve,
    required this.begin,
    required this.end,
  });
}

/// Any [Object] can act as a tween property.
typedef MovieTweenPropertyType = Object;

/// Type-safe tween property that can be used as a `const`.
class MovieTweenProperty<T> {
  MovieTweenProperty();

  /// Returns the current value of this property.
  T from(Movie movie) => movie.get(this);
}

推荐答案

试一试(animation as Map? )?["opacity"] as double?? 0

Flutter相关问答推荐

如何在容器中移动图标按钮?

Android元数据为1.9.0,预期版本为1.7.1,如何解决flutter应用程序构建失败问题?

在Flutter 中压缩图片返回空

Build.gradle专线:2个Flutter Flutter

来自FutureProvider的数据只打印两个不同的变量,它们对一个实例只有一次相同的值,为什么?

我有相关的下拉菜单Flutter Windows应用程序的问题

如何从DART中的事件列表中获取即将到来的日期

从.gitignore中排除.fvm/fvm_config.json

导航回上一页会将我带回Ffltter应用程序中的登录页面

Flutter:无法构建iOS应用程序-找不到PigeonParser.h文件

Flutter:注册 UI 出现 UI 问题(将注册框提升到顶部并向电话号码 pin 码添加一个框)

Flutter 火焰相机抖动已弃用

课程中的访问Provider

Flutter如何从深入嵌套的小部件中打开抽屉?

如何使 flutter 2 手指zoom ?

Android 模拟器在 Apple M2 芯片 EXC_BAD_ACCESS / KERN_INVALID_ADDRESS 上随机崩溃

Flutter RawMaterialButton 常量相对大小

如何在 flutter 中更改日期 Select 器 colored颜色 ?

我的主屏幕上的 FutureBuilder 不断重新加载(但仅在其他屏幕上时)?

无法列出 com.facebook.android:facebook-login -Facebook 登录问题的版本