1

I am developing Flutter plugin which provides native implementations for Flutter apps.

Is there a way that my plugin can read its own version? I've tried to integrate package_info into my plugin but it returns the host app versions and not the plugin version itself.

For example, if my plugin called my_great_plugin: ^1.0.2 I want to have an api in my plugin which returns its own version: MyGreatPlugin.instance.getVersion(); // 1.0.2

Inside the plugin, i want to have logic that read its own version from its own pubspec.yaml , and return it to the host app.

EDIT:

In addition, I've tried to use File read and also tried to define the pubspec.yaml itself as an asset of the plugin and then tried to access from code using rootBundle but it didn't worked.

Please refer to my plugin structure and its yaml file..

For example code:

class MyGreatPlugin extends GreatPluginPlatform { 
static MyGreatPlugin? _instance;

MyGreatPlugin._();

static MyGreatPlugin get instance => _instance ??= MyGreatPlugin._();

//MyGreatPlugin apis...

@override
Future getPluginVersion() async {
  
  return await rootBundle.loadString('pubspec.yaml'); //This didn't worked
  File file = new File("pubspec.yaml"). //This didn't worked either.. 

  return "1.1.0"; //Right now I am returning hardcoded version
  }
}

Plugin yaml:

name: my_great_plugin
description: Great Flutter plugin. Use our Flutter plugin to 
integrate native logics.
version: 1.1.1 //This what i want to get from code..
homepage: https://..

environment:
  sdk: ">=2.12.0 <3.0.0"
  flutter: ">=1.17.0"

dependencies:
  flutter:
    sdk: flutter
    plugin_platform_interface: ^1.1.1
    yaml: ^3.1.0

dev_dependencies:
  flutter_test:
  sdk: flutter


flutter:
  plugin:
    platforms:
      android:
        package: com.great_plugin
        pluginClass: AndroidFlutterPlugin
    ios:
       pluginClass: IosFlutterPlugin

  assets:
    - pubspec.yaml
Moti Bartov
  • 3,454
  • 33
  • 42

2 Answers2

2

You can access assets from packages by prefixing the path with packages/<package_name>/ (see this answer https://stackoverflow.com/a/63914820/14406250)

So you could do something like this from your plugin

Future<String> getVersion() async {
  final fileContent = await rootBundle.loadString(
    "packages/my_package_name/pubspec.yaml",
  );
  final pubspec = Pubspec.parse(fileContent);
  return pubspec.version!.canonicalizedVersion;
}
Matteo Melis
  • 176
  • 1
  • 9
1

Finally thanks to @Matteo Melis answer it worked!

@override
Future getPluginVersion() async {
final fileContent = await rootBundle.loadString(
  "packages/my_package/pubspec.yaml",
);

if(fileContent.isNotEmpty){
  String versionStr = fileContent.substring(fileContent.indexOf("version:"), fileContent.length);
  versionStr = versionStr.substring(0, versionStr.indexOf("\n"));
  print("getPluginVersion: fileContent: $fileContent");
  return versionStr.substring(versionStr.indexOf(":") + 1, versionStr.length).trim();
}
return null;

}

You may use some Yaml package for parsing the yaml easily but for my use-case, I've only needed to get the version so I preferred not to use any dependencies so it won't have any conflicts with hosting app.

Moti Bartov
  • 3,454
  • 33
  • 42