14

Is there any way to add a plugin only for a particular platform (like just for iOS) in pubspec.yaml file?

Something like this.

dependencies:
  flutter:
    sdk: flutter

  isIos ? http: ^1.0 : null

PS: I also know I can import the plugin normally and in code, I can make changes but I don't want this solution.

if (Theme.of(context).platform != TargetPlatform.iOS) {
  // don't use that plugin part
}

I simply don't want to add plugin in my pubspec.yaml for a particular platform. Possible?

  • Nope, please see https://stackoverflow.com/questions/55567988/flutter-audio-service-dependancy-issue-in-ios (but there may be a possible hack, posted there) – TWL Jan 24 '20 at 18:38

3 Answers3

2

For the pubspec.yaml, you wouldn't be able to specify the platform under dependencies: so

dependencies:
   flutter:
    sdk: flutter
   http: ^1.0

Then inside your dart code,

import 'dart:io';

if (Platform.isAndroid) {
  //code here will run only when device is android
} else if (Platform.isIOS) {
  //code here will run only when device is iOS
}
saytoonz
  • 186
  • 6
  • That won't work if the package is preventing the build. I am trying to use meta_seo, but I am having a hard time finding a way of having the same source code for web and for Android (excluding the package) – Guillem Poy Dec 18 '22 at 12:55
-1

Adding a plugin for only a specific platform is not possible. There is an easier way to access the current platform though.

import 'dart:io';


if(Platform.isIOS) {
 // Use plugin
}
 
mskolnick
  • 1,062
  • 8
  • 12
-1

Not sure, but one possible way around is to install that pod natively and write its implementation in Xcode, and then invoking that native code via the flutter application.

robben
  • 637
  • 1
  • 7
  • 14