【问题标题】:Error: The class 'SingleTickerProviderStateMixin' can't be used as a mixin because it extends a class other than Object错误:类“SingleTickerProviderStateMixin”不能用作混合,因为它扩展了 Object 以外的类
【发布时间】:2018-07-15 09:06:52
【问题描述】:

刚学flutter动画。使用 SingleTickerProviderStateMixin IDE 给我这个错误:

类 'SingleTickerProviderStateMixin' 不能用作 mixin,因为 它扩展了 Object 以外的类

我的代码:

  import 'package:flutter/material.dart';

  class AnimationControllerOutputBody extends StatefulWidget with  {
    @override
    _AnimationControllerOutputBodyState createState() =>
        new _AnimationControllerOutputBodyState();
  }

  class _AnimationControllerOutputBodyState extends State<AnimationControllerOutputBody> with SingleTickerProviderStateMixin {

    AnimationController animation;

    @override
    void initState() {
      super.initState();
      animation = new AnimationController(
        vsync: this,
        duration: new Duration(seconds: 3),
      );
      animation.addListener(() {
        this.setState(() {});
      });
    }

    @override
    Widget build(BuildContext context) {
      return new GestureDetector(
        child: new Center(
          child: new Text(
            animation.isAnimating
                ? animation.value.toStringAsFixed(3)
                : "Tap me!",
            style: new TextStyle(
              fontSize: 50.0,
            ),
          ),
        ),
        onTap: () {
          animation.forward(from: 0.0);
        },
      );
    }

    @override
    void dispose() {
      animation.dispose();
      super.dispose();
    }
  }

我的代码有什么问题?

【问题讨论】:

  • 为什么AnimationControllerOutputBody 上有一个with
  • 我在这里找到了这个:sergiandreplace.com/flutter-animations-the-basics。只是运行他们的代码。但收到此错误。
  • 你的链接没有这样的with
  • 检查帖子中间。或使用此类搜索。 “基本示例”标题
  • 我说的是AnimationControllerOutputBody而不是_AnimationControllerOutputBodyState

标签: android flutter


【解决方案1】:

添加到analysis_options.yaml

analyzer:
  language:
    enableSuperMixins: true

另见https://github.com/flutter/flutter/blob/master/analysis_options.yaml#L24

【讨论】:

  • 有趣。为什么需要这个?
  • 工作!再次感谢^_^
  • 这是 Dart 团队希望在 Dart 2.0 之后删除和重新设计 mixin 的实验性功能,但他们暂时将其保留为实验性形式,因为 Flutter 依赖于它。
  • @GünterZöchbauer 这里谈到的“超级混合的新 mixin 语法”是什么:github.com/flutter/flutter/pull/22870/files
  • 这取代了我的答案所指的旧的实验性 superMixin 实现。
【解决方案2】:

我使用SingleTickerProviderStateMixin,请使用TickerProviderStateMixin而不是SingleTickerProviderStateMixin

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    • 1970-01-01
    • 1970-01-01
    • 2020-02-19
    • 2018-02-01
    • 1970-01-01
    相关资源
    最近更新 更多