BytesMapBitmap constructor

BytesMapBitmap(
  1. Uint8List byteData, {
  2. MapBitmapScaling bitmapScaling = MapBitmapScaling.auto,
  3. double? width,
  4. double? height,
  5. double? imagePixelRatio,
})

Constructs a BytesMapBitmap that is created from an array of bytes that must be encoded as PNG in Uint8List.

The byteData represents the image in a PNG format, which will be decoded and rendered by the platform. The optional width, height or imagePixelRatio parameters are used to correctly scale the image for display, taking into account the devices pixel ratio.

bitmapScaling controls the scaling behavior:

  • MapBitmapScaling.auto automatically upscales and downscales the image to match the device's pixel ratio or the specified dimensions, maintaining consistency across devices.
  • MapBitmapScaling.none disables automatic scaling, which is useful when performance is a concern or if the asset is already scaled appropriately.

The imagePixelRatio parameter allows to give correct pixel ratio of the image. If the imagePixelRatio is not provided, value is defaulted to the natural resolution of 1.0. To render the asset as sharp as possible, set the imagePixelRatio to the devices pixel ratio. imagePixelRatio is ignored if width or height is provided.

Optionally, width and height can be specified to control the dimensions of the rendered image:

  • If both width and height are non-null, the image will have the specified dimensions, which might distort the original aspect ratio, similar to BoxFit.fill.
  • If only one of width and height is non-null, then the output image will be scaled to the associated width or height, and the other dimension will take whatever value is needed to maintain the image's original aspect ratio. These cases are similar to BoxFit.fitWidth and BoxFit.fitHeight, respectively.

Throws an AssertionError if byteData is empty or if incompatible scaling options are provided.

The following example demonstrates how to create an BytesMapBitmap from a list of bytes in Uint8List format:

Uint8List byteData = await _loadImageData('path/to/image.png');
double imagePixelRatio = 2.0; // Pixel density of the image.
BytesMapBitmap bytesMapBitmap = BytesMapBitmap(
  byteData,
  imagePixelRatio: imagePixelRatio,
);

Optionally, width and height can be specified to control the asset's dimensions:

Uint8List byteData = imageBuffer.asUint8List()
BytesMapBitmap bytesMapBitmap = BytesMapBitmap(
  byteData,
  width: 64, // Desired width in logical pixels.
);

To render the bitmap as sharply as possible, set the imagePixelRatio to the device's pixel ratio. This renders the asset at a pixel-to-pixel ratio on the screen, but may result in different logical marker sizes across devices with varying pixel densities.

Uint8List byteData = imageBuffer.asUint8List()
BytesMapBitmap bytesMapBitmap = BytesMapBitmap(
  byteData,
  imagePixelRatio: MediaQuery.maybeDevicePixelRatioOf(context),
);

Implementation

BytesMapBitmap(
  this.byteData, {
  super.bitmapScaling = MapBitmapScaling.auto,
  super.width,
  super.height,
  double? imagePixelRatio,
})  : assert(byteData.isNotEmpty,
          'Cannot create BitmapDescriptor with empty byteData.'),
      assert(
          bitmapScaling != MapBitmapScaling.none || imagePixelRatio == null,
          'If bitmapScaling is set to MapBitmapScaling.none, imagePixelRatio parameter cannot be used.'),
      assert(bitmapScaling != MapBitmapScaling.none || width == null,
          'If bitmapScaling is set to MapBitmapScaling.none, width parameter cannot be used.'),
      assert(bitmapScaling != MapBitmapScaling.none || height == null,
          'If bitmapScaling is set to MapBitmapScaling.none, height parameter cannot be used.'),
      super._(imagePixelRatio: imagePixelRatio ?? _naturalPixelRatio);