fromAsset static method

Future<BitmapDescriptor?> fromAsset(
  1. BuildContext context,
  2. String asset, {
  3. int? resizedWidth,
  4. int? resizedHeight,
})

Implementation

static Future<BitmapDescriptor?> fromAsset(BuildContext context, String asset,
    {int? resizedWidth, int? resizedHeight}) async {
  /// assets load from URL uses actual pixels, which may appear smaller
  /// for device with high device pixel ratio.
  if (Utils.isUrl(asset)) {
    try {
       // If the asset is available locally, then use local path
      String assetName = Utils.getAssetName(asset);
      if (Utils.isAssetAvailableLocally(assetName)) {
          return BitmapDescriptor.fromAssetImage(
        ImageConfiguration.empty, Utils.getLocalAssetFullPath(asset));
      }
      // use cache manager
      final File file = await DefaultCacheManager().getSingleFile(asset);
      final Uint8List imageBytes = await file.readAsBytes();

      if (!kIsWeb) {
        return _resizeImageWithDeviceRatio(
            imageBytes, MediaQuery.of(context).devicePixelRatio,
            resizedWidth: resizedWidth, resizedHeight: resizedHeight);
      } else {
        // Web uses ratio 1.0, nothing to do here
        return BitmapDescriptor.fromBytes(imageBytes);
      }
    } catch (e) {
      // do nothing
    }
  }

  /// local asset already handle device pixels natively
  else {
    return BitmapDescriptor.fromAssetImage(
        ImageConfiguration.empty, Utils.getLocalAssetFullPath(asset));
  }
  return null;
}