Reputation: 315
I'm trying to fix errors on my project after migration to null safety . i have error on this line :
opacity: animation["opacity"],
child: Transform.translate(
offset: Offset(0, animation["translateY"] ), child: child),
error : The method '[]' can't be unconditionally invoked because the receiver can be 'null'
My 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: -130.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),
),
);
}
}
How I can fix it ?
Thanks in advance
Upvotes: 0
Views: 726
Reputation: 8288
It means your object animation
might be null
(I guess its type is Map?
).
So you cannot do animation['yourKey']
directly as null['yourKey']
is not valid.
You have several solutions:
animation
to Map
if possible.animation
has to be Map?
but you are sure it won't be null
when you use it, you can doanimation!['yourKey']
null
then you can do:animation?['yourKey'], // If it is okay to use a `null` value here
animation?['yourKey'] ?? defaultValue, // If you need to use a non-null value
For example, for you opacity you can to:
opacity: animation?["opacity"] ?? 0,
Upvotes: 2
Reputation: 1285
if your using nullsafety
final double? delay;
final Widget? child;
then for calling them will be end at !
e.g delay! or child!
Upvotes: 0