JaffaKetchup
JaffaKetchup

Reputation: 1631

Can I see if another dart/flutter package is installed?

I am planning to make a package that has x functionality enabled. However, if package p is installed, how can I make my package enable y functionality?

I cannot have package p give any info to my package, so having them cross-communicate in some way wouldn't work.


Therefore, in Dart (with or without Flutter), can I programatically check to see if another Dart/Flutter package has been installed in the project (added to pubspec.yaml and available)?

Upvotes: 0

Views: 1047

Answers (2)

JaffaKetchup
JaffaKetchup

Reputation: 1631

I achieved this through extension methods and a seperate export/import file for 'plugins'/modules/extensions only.
Unfortunately, this won't allow for new non-methods, such as variables, but getters may be used instead.

To implement a similar system:

  1. Add a mostly empty object to the primary library
    It should include a reference variable/getter to the 'root' object of the primary library, or whatever object the plugin requires.
    See this for an example implementation
  2. Create an extension method for the object above to the secondary library
    See this for an example implementation
  3. In the primary library, create a new export/import file that the secondary library can use

Upvotes: 0

Brett Sutton
Brett Sutton

Reputation: 4554

You can determine if a package is globally installed by checking if a directory exists: (pub cache)/global_packages/package_name.

The DCli api has the following method:

DartSdk().isPackageGloballyActivated(String package)

So to check if dcli is globally active:

DartSdk().isPackageGloballyActivated('dcli');

Upvotes: 2

Related Questions