I'm following this speed code tutorial(
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();