How to duplicate repeating items inside a Dart list?

Technology CommunityCategory: FlutterHow to duplicate repeating items inside a Dart list?
VietMX Staff asked 4 years ago
Problem

Consider the code:

final List<Ball> _ballList =[Ball(),Ball(),Ball(),Ball(),Ball(),]

What can to be done in order to not repeat Ball() multiple times.

Using collection-for if we need different instances of Ball()

final List<Ball> _ballList = [
  for (var i = 0; i < 5; i += 1) Ball(),
];

If we need the same instance of Ball()List.filled should be used

final List<Ball> _ballList = List<Ball>.filled(5, Ball());