动画控制器 vsync 未将“this”识别为有效表达式
Posted
技术标签:
【中文标题】动画控制器 vsync 未将“this”识别为有效表达式【英文标题】:Animation controller vsync not recognizing 'this' as valid expression 【发布时间】:2019-10-01 22:28:57 【问题描述】:尝试在 dart 中填写 TextFormField 时实现加载动画。声明 AnimationController 时,我的声明无法识别“this”。
我发现的每个文档都使用“this”作为 vsync,由于某种原因我无法识别它。
class _SettingsScreenState extends State<SettingsScreen> with SingleTickerProviderStateMixin
final _animationController = AnimationController(vsync: this, duration: Duration(seconds: 1));
...
ListTile(
title: new TextFormField(
controller: _serverController,
keyboardType: TextInputType.emailAddress,
style: TextStyle(
color: darkGray,
fontSize: 18,
fontWeight: FontWeight.w500,
fontFamily: mainFontFamily
),
onFieldSubmitted: _handleSubmitted,
autocorrect: false,
decoration: new InputDecoration(
icon: AnimatedIcon(icon: AnimatedIcons.search_ellipsis, progress: _animationController, color: darkGray,), /*TODO get this to actually show up and animate while checking server*/
contentPadding: EdgeInsets.symmetric(
vertical: 12, horizontal: 12),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
),
),
),
),
颤振医生 -v 输出
[√] Flutter (Channel stable, v1.2.1, on Microsoft Windows [Version 10.0.17763.475], locale en-US)
• Flutter version 1.2.1 at C:\flutter
• Framework revision 8661d8aecd (3 months ago), 2019-02-14 19:19:53 -0800
• Engine revision 3757390fa4
• Dart version 2.1.2 (build 2.1.2-dev.0.0 0a7dcf17eb)
[!] android toolchain - develop for Android devices (Android SDK version 28.0.3)
• Android SDK at C:\Users\amehta\AppData\Local\Android\sdk
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-28, build-tools 28.0.3
• Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)
! Some Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses
[√] Android Studio (version 3.4)
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin version 35.3.1
• Dart plugin version 183.6270
• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)
[!] VS Code, 64-bit edition (version 1.21.0)
• VS Code at C:\Program Files\Microsoft VS Code
X Flutter extension not installed; install from
https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter
[√] Connected device (1 available)
• GT N7100 • 4d00db7fb27a90e9 • android-arm • Android 4.4.2 (API 19)
! Doctor found issues in 2 categories.
我希望图标显示出来,一旦我在 TextFormField 中提交,它将运行动画,直到服务器检查完成
【问题讨论】:
【参考方案1】:您必须将该行放在 initState 方法中,如下所示:
@override
void initState()
super.initState();
_controller = AnimationController(duration: expandDuration, vsync: this);
希望对您有所帮助。
【讨论】:
哇。是的,这行得通。我不知道我是怎么错过这个细节的。我应该删除这个问题还是将其标记为已回答? 留着吧,说不定能帮到别人。当你对 Dart 不太熟悉时,这可能是一个常见的错误。 你能解释一下为什么 initState() 中的双关语有效吗?而不是在 build()以上是关于动画控制器 vsync 未将“this”识别为有效表达式的主要内容,如果未能解决你的问题,请参考以下文章