Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Animation controller Invalid reference to 'this' expression

Tags:

flutter

dart

I am using flutter spinkkit plugin issue is its showing an error on vsync this. Invalid reference to 'this' expression.

final spinkit = SpinKitSquareCircle( color: kPrimaryColor, size: 50.0, controller: AnimationController(vsync: this, duration: const Duration(milliseconds: 1200)), );

So somewhere I read that I need to place it inside init state

So if I place this in init state its showing the error Undefined name 'spinkit'. where I am using spinner.

like image 310
rameez khan Avatar asked Jun 14 '26 16:06

rameez khan


2 Answers

for Invalid reference to 'this' expression. you have to add SingleTickerProviderStateMixin you can check it below.

class test extends StatefulWidget {
  @override
  _testState createState() => _testState();
}

class _testState extends State<test> with SingleTickerProviderStateMixin{

 @override
  void initState() {
    // TODO: implement initState
    super.initState();

    final spinkit = SpinKitSquareCircle(
      color: kPrimaryColor,
      size: 50.0,
      controller: AnimationController(
          vsync: this, duration: const Duration(milliseconds: 1200)),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}
like image 96
Avinash Avatar answered Jun 16 '26 07:06

Avinash


First, you have to extend your class with SingleTickerProviderStateMixin. This enables you to use AnimationController. And if you already extend a class with the above provider then you can try following answer. Make sure to make your spinkit variable late final as well.

You have to place that line inside initState method, as follow:

late final spinkit;

@override
  void initState() {
    super.initState();
    spinkit = SpinKitSquareCircle( color: kPrimaryColor, size: 50.0, controller: AnimationController(vsync: this, duration: const Duration(milliseconds: 1200)), );

}
like image 24
Shubham Narkhede Avatar answered Jun 16 '26 09:06

Shubham Narkhede