参数类型“_ProfileImageState”不能分配给参数类型“TickerProvider”

主要目标是允许用户从图库/相机中选择图像,但为了使事情看起来不错,使用了一些动画并设计了整个图像选择对话框

class _ProfileImageState extends State<ProfileImage>{

      @override

      File _image;

      //ImagePickerHandler imagePicker;

      @override
      void initState() {

        super.initState();
//initiating to start so that transition from one state to another is smooth

        var _controller = new AnimationController(
          vsync: this,
          duration: const Duration(milliseconds: 500),
        );

        imagePicker=new ImagePickerHandler(this.userImage(_image));

      }
stack overflow The argument type '_ProfileImageState' can't be assigned to the parameter type 'TickerProvider'
原文答案

答案:

作者头像

您需要使用 SingleTickerProviderStateMixin mixin 才能将 this 用作 vsync 参数。您可以通过使用 with 关键字来实现此目的,方法如下:


class _ProfileImageState extends State<ProfileImage> with SingleTickerProviderStateMixin {

      @override

      File _image;

      //ImagePickerHandler imagePicker;

      @override
      void initState() {

        super.initState();

        var _controller = new AnimationController(
          vsync: this,
          duration: const Duration(milliseconds: 500),
        );

        imagePicker=new ImagePickerHandler(this.userImage(_image));

      }``` 

 `` `
作者头像

扩展 SingleTickerProviderStateMixin 解决了这个问题:

类 _ProfileImageState 使用 SingleTickerProviderStateMixin 扩展状态{

相关问题