Vertices constructor Null safety

Vertices(
  1. VertexMode mode,
  2. List<Offset> positions,
  3. {List<Offset>? textureCoordinates,
  4. List<Color>? colors,
  5. List<int>? indices}
)

Creates a set of vertex data for use with Canvas.drawVertices.

The mode and positions parameters must not be null. The positions parameter is a list of triangular mesh vertices(xy).

If the textureCoordinates or colors parameters are provided, they must be the same length as positions.

The textureCoordinates parameter is used to cutout the image set in the image shader. The cut part is applied to the triangular mesh. Note that the textureCoordinates are the coordinates on the image.

If the indices parameter is provided, all values in the list must be valid index values for positions. e.g. The indices parameter for a simple triangle is 0,1,2.

Implementation

Vertices(
  VertexMode mode,
  List<Offset> positions, {
  List<Offset>? textureCoordinates,
  List<Color>? colors,
  List<int>? indices,
}) : assert(mode != null),
     assert(positions != null) {
  if (textureCoordinates != null && textureCoordinates.length != positions.length) {
    throw ArgumentError('"positions" and "textureCoordinates" lengths must match.');
  }
  if (colors != null && colors.length != positions.length) {
    throw ArgumentError('"positions" and "colors" lengths must match.');
  }
  if (indices != null && indices.any((int i) => i < 0 || i >= positions.length)) {
    throw ArgumentError('"indices" values must be valid indices in the positions list.');
  }

  final Float32List encodedPositions = _encodePointList(positions);
  final Float32List? encodedTextureCoordinates = (textureCoordinates != null)
    ? _encodePointList(textureCoordinates)
    : null;
  final Int32List? encodedColors = colors != null
    ? _encodeColorList(colors)
    : null;
  final Uint16List? encodedIndices = indices != null
    ? Uint16List.fromList(indices)
    : null;

  if (!_init(this, mode.index, encodedPositions, encodedTextureCoordinates, encodedColors, encodedIndices)) {
    throw ArgumentError('Invalid configuration for vertices.');
  }
}