0

i know the solution is simple but im new to nullsafety and animations please help me solve this problem :

here is the code :

class FadeAnimation extends StatelessWidget {
  final double delay;
  final Widget child;

  FadeAnimation(this.delay, this.child);

  @override
  Widget build(BuildContext context) {
    final tween = MultiTrackTween([
      Track("opacity")
          .add(Duration(milliseconds: 500), Tween(begin: 0.0, end: 1.0)),
      Track("translateY").add(
          Duration(milliseconds: 500), Tween(begin: -30.0, end: 0.0),
          curve: Curves.easeOut)
    ]);

    return ControlledAnimation(
      delay: Duration(milliseconds: (500 * delay).round()),
      duration: tween.duration,
      tween: tween,
      child: child,
      builderWithChild: (context, child, animation) => Opacity(
        opacity: animation["opacity"],
        child: Transform.translate(
            offset: Offset(0, animation["translateY"]), child: child),
      ),
    );
  }
}

the problem is with thise 2 lines

opacity: animation["opacity"],

and this one :

 offset: Offset(0, animation["translateY"]), child: child),

Note: the null operator didnt worked . i appreciate your help inadvance .

delaram
  • 649
  • 4
  • 8
  • 15

1 Answers1

0
  1. Seems like Map, which from you retrieving values is nullable.
  2. Opacity widget has required field opacity, which cannot be null
  3. Offset has required field too, which also cannot be null

So you have to convert these values to non nullabe, for example:

  Opacity(
    opacity: animation?["opacity"] ?? .1, //we return default value (any suitable default value, for example 0.1) if animation["opacity"] returns null
    child: Transform.translate(
        offset: Offset(0, animation?["translateY"] ?? .1), child: child), //same
  )
Autocrab
  • 3,474
  • 1
  • 15
  • 15
  • Hi thanks or your attention... but it doesnt work ......it says : error: The operator '[]' isn't defined for the type 'Object'. how can i solve this problem ? – delaram Jun 21 '21 at 18:24
  • i guess the main problem is this : info: The library package:simple_animations/simple_animations.dart' is legacy, and should not be imported into a null safe library. I have to change the version or code ....... – delaram Jun 21 '21 at 18:25