Vertices.raw constructor Null safety

Vertices.raw(
  1. VertexMode mode,
  2. Float32List positions,
  3. {Float32List? textureCoordinates,
  4. Int32List? colors,
  5. Uint16List? indices}
)

Creates a set of vertex data for use with Canvas.drawVertices, directly using the encoding methods of Vertices.new. Note that this constructor uses raw typed data lists, so it runs faster than the Vertices() constructor because it doesn't require any conversion from Dart lists.

The mode parameter must not be null.

The positions parameter is a list of triangular mesh vertices and is interpreted as a list of repeated pairs of x,y coordinates. It must not be null.

The textureCoordinates list is interpreted as a list of repeated pairs of x,y coordinates, and must be the same length of positions if it is not null. 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.

The colors list is interpreted as a list of ARGB encoded colors, similar to Color.value. It must be half length of positions if it is not null.

If the indices list 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.raw(
  VertexMode mode,
  Float32List positions, {
  Float32List? textureCoordinates,
  Int32List? colors,
  Uint16List? 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 * 2 != 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.');
  }

  if (!_init(this, mode.index, positions, textureCoordinates, colors, indices)) {
    throw ArgumentError('Invalid configuration for vertices.');
  }
}