The argument type '_MyHomePageState' can't be assigned to the parameter type 'TickerProvider'.
时间: 2024-01-11 14:05:36 浏览: 275
LHS.zip_ARGUMENT!_LHS matlab_LHS采样_dist.m_拉丁 抽样
This error message is typically encountered when trying to pass a stateful widget as a TickerProvider to an animation controller.
To fix the error, you can either create a new class that extends TickerProvider or use the default TickerProvider that comes with the AnimationController class.
For example, if you have a MyHomePageState class that extends StatefulWidget, you can create a new class that extends TickerProvider like this:
```
class MyHomePageTickerProvider extends TickerProvider {
@override
Ticker createTicker(onTick) => Ticker(onTick);
}
```
Then, you can pass an instance of this new class to your animation controller like this:
```
AnimationController(
vsync: MyHomePageTickerProvider(),
duration: Duration(seconds: 1),
);
```
Alternatively, you can use the default TickerProvider that comes with the AnimationController class by passing `vsync: this` if your stateful widget extends TickerProviderStateMixin.
```
class MyHomePage extends StatefulWidget with TickerProviderStateMixin {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
child: Text('Hello World'),
),
),
);
}
@override
void dispose() {
super.dispose();
}
}
```
Then, you can pass `vsync: this` to your animation controller like this:
```
AnimationController(
vsync: this,
duration: Duration(seconds: 1),
);
```
阅读全文