Search code examples
flutterdartanimationcontroller

Flutter 2.2.3 : The argument type 'Animation<dynamic>' can't be assigned to the parameter type 'Animation<double>'


I tried to pass list position from this and get this error : The argument type 'Animation' can't be assigned to the parameter type 'Animation'. I follow the steps from a youtuber, and can check full code here:https://github.com/codespiration/petadoption/blob/master/menu_frame.dart . Really appreciate you guys help

> @override   void initState() {
>     super.initState();
>     _animationController = AnimationController(vsync: this, duration: duration);
>     scaleAnimation =
>         Tween<double>(begin: 1.0, end: 0.6).animate(_animationController);
>     smallerScaleAnimation =
>         Tween<double>(begin: 1.0, end: 0.5).animate(_animationController);
> 
>     scaleAnimations = [
>       Tween<double>(begin: 1.0, end: 0.7).animate(_animationController),
>       Tween<double>(begin: 1.0, end: 0.6).animate(_animationController),
>       Tween<double>(begin: 1.0, end: 0.5).animate(_animationController),
>     ];
>     _animationController.forward();   }
> 
> Widget buildScreenStack(int position) {
>     final deviceWidth = MediaQuery.of(context).size.width;
>     return AnimatedPositioned(
>       duration: duration,
>       top: 0,
>       bottom: 0,
>       left: menuOpen ? deviceWidth * 0.35 : 0.0,
>       right: menuOpen ? deviceWidth * -0.65 : 0.0,
>       child: ScaleTransition(
>         scale: scaleAnimations[position],
>         child: GestureDetector(
>           onTap: () {
>             if (menuOpen) {
>               setState(() {
>                 menuOpen = false;
>                 _animationController.reverse();
>               });
>             }
>           },
>           child: AbsorbPointer(
>             absorbing: menuOpen,
>             child: Stack(
>               children: <Widget>[
>                 Material(
>                   animationDuration: duration,
>                   borderRadius: BorderRadius.circular(menuOpen ? 30.0 : 0.0),
>                   child: screens[position],
>                 ),
>               ],
>             ),
>           ),
>         ),
>       ),
>     );   }
> 
>   @override   Widget build(BuildContext context) {
>     final deviceWidth = MediaQuery.of(context).size.width;
>     return Stack(
>       children: finalStack(),
>     );   } }

Solution

  • The scaleAnimations field is declared as List<Animation>, and since Animation is generic, this is implicitly the same as List<Animation<dynamic>>. So the runtime will interpret any elements of the list as Animation<dynamic>, even if they actually have the type Animation<double>.

    To fix this, you just need to change the field declaration:

    List<Animation<double>> scaleAnimations;