Flutter 在 null 上调用了 getter 'value'。 / 接收者:空 / 尝试调用:值

时间:2021-04-06 15:51:50

标签: flutter

我正在为 360 度图像制作动画,我编写代码并初始化一些属性 以下是代码:

import 'package:flutter/material.dart';
import 'dart:math' as math;

class BasicAnimations extends StatefulWidget {
  @override
  _BasicAnimationsState createState() => _BasicAnimationsState();
}

class _BasicAnimationsState extends State<BasicAnimations>
    with SingleTickerProviderStateMixin {

  Animation<double> _animation;
  AnimationController _animationController;

  @override
  void initState() {
    _animationController = AnimationController(duration: Duration(milliseconds: 1500) ,vsync: this);

    _animation = Tween<double>(begin: 0, end: 2 * math.pi).animate(_animationController);
    _animationController.forward();

    super.initState();
  }

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

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: Text("Basic Animations"),
      ),

所以在正文中:Transform.rotate 我设置了 _animation.value 并且在空代码上调用了这个 thorw 错误“值”:

body: Transform.rotate(
        angle: _animation.value,
        child: Center(
          child: Container(
              width: 150,
              height: 150,
              child: Image.asset("assets/flutter_2.png")),
        ),
      ),
    );
  }
}

有什么解决办法吗?提前致谢:)

1 个答案:

答案 0 :(得分:1)

写这个:

AnimationController _animationController = AnimationController();

您需要先实例化它 AnimationController,然后调用它,就像使用 TextEditingControllerd 和其他控制器一样。