Home > Enterprise >  Can't figure out this Error "The method 'map' can't be unconditionally invo
Can't figure out this Error "The method 'map' can't be unconditionally invo

Time:02-02

I'm following this speed code tutorial(result

CodePudding user response:

You are using nullable dataType List<Color>? colors;, So it is possible to get null.

You can pass empty list on null case

    List<Color> gradColors = colors
            ?.map(
              (color) => color.withOpacity(
                Random().nextDouble().clamp(0.5, 0.9),
              ),
            )
            .toList() ??
        [];

CodePudding user response:

You can use Yeasin Sheikh's answer, but here are two more ways:

List<Color> gradColors = colors == null ? [] : colors!.map((color) => color.withOpacity(Random().nextDouble().clamp(0.5, 0.9))).toList();

or

List<Color> gradColors = (colors ?? []).map((color) => color.withOpacity(Random().nextDouble().clamp(0.5, 0.9))).toList();
  • Related