We want to define a Flutter asset that can be accessed from the Android platform code within the same plugin. The documentation for this is purported to be here.
Please note we are NOT talking about custom platform code within a flutter app -- we are talking about flutter plugin code from the perspective of the plugin author.
So, following the docs, we start with the pubspec.yaml file for our plugin which exists at [your_plugin_root_dir]\pubspec.yaml, and we add the asset definition:
assets:
- assets/sound.wav
This points to an existing file at [your_plugin_root_dir]\assets\sound.wav
All good so far, but then we have a problem:
When you create a new Flutter Plugin project in Android studio, the Android Java file for the plugin looks like this:
public class PluginTestPlugin implements FlutterPlugin, MethodCallHandler {
/// The MethodChannel that will the communication between Flutter and native Android
///
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
/// when the Flutter Engine is detached from the Activity
private MethodChannel channel;
@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "plugin_test");
channel.setMethodCallHandler(this);
}
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
if (call.method.equals("getPlatformVersion")) {
result.success("Android " + android.os.Build.VERSION.RELEASE);
} else {
result.notImplemented();
}
}
@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
}
}
And in order to access your asset from within that code, the docs suggest adding this code:
AssetManager assetManager = registrar.context().getAssets();
String key = registrar.lookupKeyForAsset("assets/sound.wav");
AssetFileDescriptor fd = assetManager.openFd(key);
But since there is no "registrar" instance that exists in that code, and the documentation doesn't describe how it was created or how to access it, I'm lost.
What am I missing here????