AnimationController The named parameter 'vsync' isn't defined

FlutterDartAnimationcontroller

Flutter Problem Overview


AnimationController stopped working because somehow vsync is not a named parameter anymore.

This line of code stopped working.

controller = AnimationController(duration: Duration(seconds: 3), vsync: this);

It now shows an error saying:

The named parameter 'vsync' isn't defined.
Try correcting the name to an existing named parameter's name, or defining a named parameter with the name 'vsync'.dartundefined_named_parameter

I use this exact same code in two different apps, in one of them I just removed the fading text animation as a hot fix, but on the other app I need a real fix. Has anyone seen this problem recently?

Note:

-This exact same code has been working for months and stopped working after an update.

-The surrounding class does have: with TickerProviderStateMixin

class FadingText extends StatefulWidget {
  final String text;
  final int seconds;
  final TextStyle style;

  FadingText({this.text, this.seconds, this.style});

  @override
  _FadingTextState createState() => _FadingTextState();
}

class _FadingTextState extends State<FadingText> with TickerProviderStateMixin {
  AnimationController controller;
  Animation animation;

  @override
  Widget build(BuildContext context) {
    return Container(
      child: FadeTransition(
        opacity: animation,
        child: Text(widget.text, style: widget.style,),
      ),
    );
  }

  @override
  void initState() {
    super.initState();

    controller = AnimationController(duration: Duration(seconds: widget.seconds), vsync: this);
    animation = Tween(begin: 0.5, end: 1.0).animate(controller);

    animation.addStatusListener((status) {
      if (status == AnimationStatus.completed) { controller.reverse(); }
      else if (status == AnimationStatus.dismissed) { controller.forward(); }
    });

    controller.forward();
  }


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

}
Flutter Doctor


[✓] Flutter (Channel master, 1.21.0-6.0.pre.140, on Mac OS X 10.15.5 19F101, locale en-MX)
    • Flutter version 1.21.0-6.0.pre.140 at /Users/luisharo/Developer/flutterFramework revision 7884420a0a (25 hours ago), 2020-07-31 20:20:00 +0200Engine revision 280bbfc763Dart version 2.10.0 (build 2.10.0-2.0.dev bd528bfbd6)

 
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
    • Android SDK at /Users/luisharo/Library/Android/sdkPlatform android-29, build-tools 29.0.3Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/javaJava version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.5)
    • Xcode at /Applications/Xcode.app/Contents/DeveloperXcode 11.5, Build version 11E608cCocoaPods version 1.8.4

[✓] Chrome - develop for the webChrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 3.6)
    • Android Studio at /Applications/Android Studio.app/ContentsFlutter plugin version 45.1.1Dart plugin version 192.7761Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211)

[✓] IntelliJ IDEA Community Edition (version 2019.2.4)
    • IntelliJ at /Applications/IntelliJ IDEA CE.appFlutter plugin version 45.1.1Dart plugin version 192.8052

[✓] VS Code (version 1.47.3)
    • VS Code at /Applications/Visual Studio Code.app/ContentsFlutter extension version 3.13.1

[✓] Connected device (4 available)
    • JKM LX3 (mobile)       • 7MLNW19723003608android-arm64Android 9 (API 28)
    • iPhone 11 Pro (mobile) • 675C24C4-7682-4DFE-8037-062893405EE7ioscom.apple.CoreSimulator.SimRuntime.iOS-13-5
      (simulator)
    • Web Server (web)       • web-serverweb-javascriptFlutter ToolsChrome (web)           • chromeweb-javascriptGoogle Chrome 84.0.4147.105No issues found!

Flutter Solutions


Solution 1 - Flutter

In your pubspec.yml

change this to:

environment:
  sdk: ">=2.8.0 <3.0.0"

and flutter pub get

in Android Studio invalidate the cache and restart, you should be good to go.

Credit : https://github.com/flutter/flutter/issues/62752#issuecomment-667744745

Solution 2 - Flutter

What happened is that @required syntax was migrated during the rollout of null-safety to now just be required. This is valid new-Dart, but if you upgrade with your editor open, it will have stale build rules. Simply run flutter clean and restart your IDE, and that should do it.

Hopefully no need to restart your computer, change internet providers, etc.

Solution 3 - Flutter

A few things (unfortunately not pinpointed) that help me with issues like this:

**edit: I would recommend doing these in order, hopefully the issue will be solved before you reach the end of the list.

  • run flutter upgrade
  • run flutter pub get
  • run flutter clean
  • restart IDE
  • change flutter channel
  • and in this case, change minimum SDK version from 2.7.0 to 2.8.0.

There are, of course, caveats to a few of these tactics. More experienced developers would surely have more details to offer, but I haven't encountered issues with this approach.

Solution 4 - Flutter

Had the same issue after upgrading flutter. Restarting the IDE fixed the problem.

Solution 5 - Flutter

I ran into the same issue when I upgraded to Flutter 1.22.

I am using VS Code.

Simply run: Flutter Clean in the Terminal and restart your IDE. That should fix the problem!

Solution 6 - Flutter

if you still getting same error with vsync: this you probably don't have with TickerProviderStateMixin in your State definition

correct your state definition like this

class _BookListState extends State<BookList> with TickerProviderStateMixin {
  final BookHelper _bookHelper = BookHelper.instance;
}

Solution 7 - Flutter

I have given 5 steps and apply all the steps in the same order as mentioned and I think your problem will be solved.

Step 1: Change the SDK version in the pubspec.yaml to 2.8

environment:
sdk: '>=2.8.0 <3.0.0'

Step 2: Open terminal and run

flutter upgrade

Step 3: After Step 2, get all the package by running this command

flutter pub get 

Step 4: Then use this command to clean flutter

flutter clean   

Step 5: Close the IDE (like VS Code, Android Studio) and then reopen it. That's all!!!

vsync will appear back again.

Apart from vsync, the colors property in LinearGradient was also missing and after I applied all these steps, colors property along with vsync reappeared.

Solution 8 - Flutter

After upgrading flutter.

  1. Restarting the IDE
  2. If necessary, reboot your computer

Solution 9 - Flutter

How did I solve it in Android Studio ?

Firstly, previous answers can be wrong because I did not change my SDK range:

environment:
  sdk: ">=2.1.0<3.0.0"

I wrote code (vsync) in my dart file and I know Android Studio shows error but do not care, just write your code:

AnimationController _animationController = AnimationController(
  duration: Duration(seconds: 3),
  vsync: this,
);

After that, close your project and then open it again. System can understand by itself. That's all ! You can see my screen shot proof:

enter image description here

Solution 10 - Flutter

Just restarting the IDE solves this issue for me

and don't forget to inheritate Tickerprovider.

Solution 11 - Flutter

I was having same issue, I upgraded the SDK and then did Invalidate caches and Restart and It worked for me.

enter image description here

Solution 12 - Flutter

Don't do anything just invalid the cache and restart your IDE

  1. flutter clean
  2. restart your IDE

Solution 13 - Flutter

in my case, I resolved the issue after setting the dart and flutter SDK path in android studio.

Solution 14 - Flutter

how about add this,

class _AnimationScreenState extends State ///

with SingleTickerProviderStateMixin {

/// }

Solution 15 - Flutter

This worked for me

Change the SDK version in the pubspec.yaml to '>=2.8.0 <3.0.0'

Then Run following commands :

  • flutter upgrade
  • flutter pub get
  • flutter clean

And Restart IDE

Solution 16 - Flutter

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

@override
void initState() {
super.initState();
_controller = AnimationController(duration: expandDuration, vsync: this);
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChamanhmView Question on Stackoverflow
Solution 1 - FlutterQuentin AoustinView Answer on Stackoverflow
Solution 2 - FlutterCraig LabenzView Answer on Stackoverflow
Solution 3 - FlutterShadView Answer on Stackoverflow
Solution 4 - FlutterStefan CiobotaruView Answer on Stackoverflow
Solution 5 - FlutteriMujtaba8488View Answer on Stackoverflow
Solution 6 - FlutterFarukTView Answer on Stackoverflow
Solution 7 - FlutterGAURAV JOSHIView Answer on Stackoverflow
Solution 8 - FlutterAdonias VasquezView Answer on Stackoverflow
Solution 9 - FlutterNamelessView Answer on Stackoverflow
Solution 10 - Fluttersubhajit dasView Answer on Stackoverflow
Solution 11 - FlutterYogesh TanwarView Answer on Stackoverflow
Solution 12 - FlutterMuhammad Tameem RafayView Answer on Stackoverflow
Solution 13 - FlutterMuhammad KashifView Answer on Stackoverflow
Solution 14 - FlutterseungyongCView Answer on Stackoverflow
Solution 15 - FlutterGAURAV MOKASHIView Answer on Stackoverflow
Solution 16 - FlutterKalpesh KhandlaView Answer on Stackoverflow