getBytesFromAsset function

Future<Uint8List> getBytesFromAsset(
  1. String path, {
  2. int? width,
})

Checks the network connection by attempting to lookup 'google.com'.

Returns true if the device is connected to the internet, otherwise returns false. Loads bytes from the specified asset path.

Returns a Future containing a Uint8List representing the bytes of the asset.

The path parameter is the path to the asset.

The width parameter specifies the target width for resizing the image.

Example usage:

Uint8List bytes = getBytesFromAsset('assets/marker.png', width: 100);

Implementation

/*Future<bool> checkConnection() async {
  try {
    final result = await InternetAddress.lookup('google.com');
    if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
      return true;
    }
    return false;
  } catch (e) {
    return false;
  }
}*/

/// Loads bytes from the specified asset path.
///
/// Returns a [Future] containing a [Uint8List] representing the bytes of the asset.
///
/// The [path] parameter is the path to the asset.
///
/// The [width] parameter specifies the target width for resizing the image.
///
/// Example usage:
/// ```dart
/// Uint8List bytes = getBytesFromAsset('assets/marker.png', width: 100);
Future<Uint8List> getBytesFromAsset(String path, {int? width}) async {
  ByteData data = await rootBundle.load(path);
  if (width == null) {
    return data.buffer.asUint8List();
  }
  ui.Codec codec = await ui.instantiateImageCodec(
    data.buffer.asUint8List(),
    targetWidth: width,
  );

  ui.FrameInfo fi = await codec.getNextFrame();
  return (await fi.image.toByteData(format: ui.ImageByteFormat.png))!
      .buffer
      .asUint8List();
}