flutter: NoSuchMethodError:the method ‘-‘ was called on null

写了个Tween<Color> 报错: NoSuchMethodError:the method '-' was called on null

color = Tween<Color>(begin: Colors.green, end: Colors.red).animate(
        CurvedAnimation(
            parent: controller, curve: Interval(.0, .6, curve: Curves.ease)));

写错了, 正确写法是使用ColorTween:

color = ColorTween(begin: Colors.green, end: Colors.red).animate(
        CurvedAnimation(
            parent: controller, curve: Interval(.0, .6, curve: Curves.ease)));

class Tween 应该处理的是double类型渐变效果, 颜色渐变应使用ColorTween

class ColorTween extends Tween<Color?> {
  /// Creates a [Color] tween.
  ///
  /// The [begin] and [end] properties may be null; the null value
  /// is treated as transparent.
  ///
  /// We recommend that you do not pass [Colors.transparent] as [begin]
  /// or [end] if you want the effect of fading in or out of transparent.
  /// Instead prefer null. [Colors.transparent] refers to black transparent and
  /// thus will fade out of or into black which is likely unwanted.
  ColorTween({ Color? begin, Color? end }) : super(begin: begin, end: end);

  /// Returns the value this variable has at the given animation clock value.
  @override
  Color? lerp(double t) => Color.lerp(begin, end, t);
}

网上找到类似问题: flutter报错:NoSuchMethodError: The method '>' was called on null.

是由于 Column或者Row的children里有Flexible导致。

 

不得不说,对Flutter Tween类还是不熟悉,而且Flutter类型判断还是不完善。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值