Skip to content Skip to sidebar Skip to footer

Is There A Way To Access Flutter Resources From Native Code?

I want to use the main logo from Flutter resources in my native Android code — for example, to display a notification. As you know, in Android you usually get resources from draw

Solution 1:

There's a nice section sort-of about this in the flutter documentation.

Given an asset "icons/heart.png", for android (since 1.22):

FlutterLoader loader = FlutterInjector.instance().flutterLoader();

String key = loader.getLookupKeyForAsset("icons/heart.png");

For iOS (swift):

let key = registrar.lookupKey(forAsset: "icons/heart.png");

let path = Bundle.main.path(forResource: key, ofType: nil)

For iOS (obj-c):

NSString* key = [registrar lookupKeyForAsset:@"icons/heart.png"];

NSString* path = [[NSBundle mainBundle] pathForResource:key ofType:nil];

For android previous to Flutter version 1.22 (deprecated):

AssetManager assetManager = registrar.context().getAssets();

String key = registrar.lookupKeyForAsset("icons/heart.png");

AssetFileDescriptor fd = assetManager.openFd(key);

However, you won't be able to share a drawable item directly to flutter, and the main icon is a bit of a special case that you definitely can't share with flutter either. Flutter doesn't know what a 'drawable' is but rather deals with resources its own cross-platform way.

Solution 2:

UPDATE FOR NEW VERSION

For flutter release 1.22 or above, FlutterLoader is only accessible via FlutterInjector.

import io.flutter.FlutterInjector;

FlutterLoaderloader= FlutterInjector.instance().flutterLoader();
 Stringkey= loader.getLookupKeyForAsset("assets_file_name");

Solution 3:

You need to get relative path of asset file.

Below is the helper method to access the file/image path from swift native code.

funcflutterAssetLottieFilePath(imageName : String) -> String {
        var flutterViewController =FlutterViewController()
        flutterViewController =UIWindow.init(frame: UIScreen.main.bounds).rootViewController as!FlutterViewControllerlet key = flutterViewController.lookupKey(forAsset:"assets/images/\(imageName).png")
        let path =Bundle.main.path(forResource: key, ofType: nil)
        return path ??""
    }

Solution 4:

As of when I wrote this, the documentation was out of date. On my version of flutter, 1.17.1 here is an kotlin android example with the necessary imports above:

import android.content.Context
import io.flutter.embedding.engine.loader.FlutterLoader
import java.io.InputStream

val myAssetPath : Sting = "assets/my_asset"val assetLookupKey =  FlutterLoader.getInstance().getLookupKeyForAsset(myAssetPath)
val inputStream: InputStream = applicationContext.assets.open(assetLookupKey)

// Read in the file from the InputStream... 

It should be noted that the value returned from this getLookupKeyForAsset method is usually your asset path, as included it on your pubspec.yaml under assets, simply prefixed with "flutter_assets/". So in the case of the above example, it would return flutter_assets/assets/my_asset

Post a Comment for "Is There A Way To Access Flutter Resources From Native Code?"