Sub topic 9
Plugins that need Android, iOS, or web setup
This chapter assumes you can add a package with flutter pub add, read pubspec.yaml, and run an app on at least one emulator, simulator, or browser. It covers what to do after pub get succeeds, when a plugin also needs changes outside your Dart code.
Why some packages need more than a pub add
A pure Dart package contains only Dart code. It runs the same way everywhere Dart runs, so adding it to pubspec.yaml is enough.
A plugin is a package that also contains platform-specific code. That code might be Kotlin or Java for Android, Swift or Objective-C for iOS, or JavaScript for the web. The plugin exposes one Dart API, and underneath it calls into whichever native implementation matches the platform the app runs on.
Flutter's tooling registers the native half for you at build time. It cannot decide what your app is allowed to do. Each operating system or browser has its own rules about permissions, declared capabilities, and minimum versions. Your app must declare those rules in files the plugin cannot edit for you.
| Platform | Files you typically edit | Typical reasons |
|---|---|---|
| Android | `android/app/src/main/AndroidManifest.xml`, `android/app/build.gradle(.kts)` | Permissions, `<queries>` entries, minimum SDK |
| iOS | `ios/Runner/Info.plist`, `ios/Podfile` | Usage description strings, URL schemes, minimum iOS version |
| Web | `web/index.html` | Script tags for external SDKs, API keys |
Many plugins need no edits at all. Others need one or two. The plugin's own documentation tells you which.
Finding out what a plugin requires
Read these places on the package's pub.dev page, in this order:
- The platform tags near the top of the page. They show which of Android, iOS, web, Linux, macOS, and Windows the package supports.
- The Readme, especially headings such as "Setup", "Installation", "Platform specifics", or "Permissions".
- The Changelog, to see whether a newer version raised a minimum SDK or changed a manifest entry.
- The
example/directory. It holds a working app, so you can compare its manifest and plist files with yours.
If your target platform is missing from the tags, the plugin does not support it. Calls on that platform fail at runtime, so check this before you commit to the package.
A worked example: location with geolocator
Add the plugin:
flutter pub add geolocatorNow write the Dart side. This function checks that location services are on, asks for permission if needed, and returns the current position:
import 'package:geolocator/geolocator.dart';
Future<Position> currentPosition() async {
final serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
throw Exception('Location services are turned off.');
}
var permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
}
if (permission == LocationPermission.denied ||
permission == LocationPermission.deniedForever) {
throw Exception('Location permission was not granted.');
}
return Geolocator.getCurrentPosition();
}This code compiles as it is. Without the platform setup below, it fails on Android and crashes on iOS.
Android: declare the permission
Android refuses to grant a permission that the app has not declared. Open android/app/src/main/AndroidManifest.xml and add the entries inside <manifest>, before the <application> element:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />Fine location gives precise coordinates. Coarse location gives approximate ones. Declare only what your app needs.
iOS: explain why you need it
iOS requires a human-readable reason for each sensitive capability. Open ios/Runner/Info.plist and add a key inside the top-level <dict>:
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app uses your location to show nearby places.</string>The system shows this string in the permission prompt. If the key is missing, iOS terminates the app the moment it requests the permission. The crash happens at runtime, not at build time, so it is easy to miss if you test only on Android.
Web: nothing to edit, with conditions
The web implementation uses the browser's Geolocation API. It needs no entries in web/index.html. Browsers expose location only on secure origins, meaning HTTPS or localhost. A page served over plain HTTP from another host will be refused.
Web setup: script tags and API keys
Some web implementations wrap a JavaScript SDK that you must load yourself. The Google Maps plugin is the common case. Its Readme tells you to add a script tag to web/index.html:
<head>
<base href="$FLUTTER_BASE_HREF">
<meta charset="UTF-8">
<title>my_app</title>
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY"></script>
</head>Replace YOUR_API_KEY with your own key, and restrict that key to your domains in the provider's console. Anything in web/index.html is public once deployed.
Raising minimum versions
A plugin's native code can depend on newer operating system features than your project's defaults allow. The error appears during the native build.
On Android, set the minimum SDK in android/app/build.gradle.kts (or build.gradle in older projects):
android {
defaultConfig {
minSdk = 23
}
}The default value is flutter.minSdkVersion. Replace it with a number only when the plugin documents a higher requirement. A typical failure message is Manifest merger failed : uses-sdk:minSdkVersion 21 cannot be smaller than version 23.
On iOS, set the platform line in ios/Podfile:
platform :ios, '13.0'Use the version the plugin documents. If the line is commented out, remove the # and set the version. After any Podfile change, run this on a Mac:
cd ios
pod install
cd ..The workflow end to end
Rebuild, do not hot reload
Native code and native configuration are compiled into the app binary. Hot reload and hot restart only replace Dart code. After you add a plugin or edit a manifest, plist, Gradle file, or Podfile, stop the app and run it again with flutter run.
If a build still behaves as if your change is missing, clear the build state:
flutter clean
flutter pub get
flutter runCommon errors
MissingPluginException(No implementation found for method ...). The native half was not registered in the running app. You most likely added the plugin and used hot restart. Stop and rebuild. It also appears when you run on a platform the plugin does not support.UnimplementedErroror "not supported on this platform". The plugin has no implementation for your target. Check the platform tags before choosing the package.- iOS app closes immediately when a feature is used. A usage description key is missing from
Info.plist. The Xcode console names the missing key. - Permission request returns
deniedevery time on Android. Theuses-permissionentry is missing, or it is placed inside<application>instead of directly under<manifest>. - Manifest merger failed on Android. A minimum SDK or an attribute conflicts with the plugin's own manifest. Raise
minSdk, or read the merger message, which names the conflicting library. - CocoaPods errors on iOS. Run
pod installinsideios/. If specs are outdated,pod repo updatemay help. This only works on macOS. - A feature works in the emulator but not in the browser. The plugin may need HTTPS, a script tag, or a browser permission prompt that your test page blocks.
Keeping setup maintainable
Commit your android/, ios/, and web/ changes together with the pubspec.yaml and pubspec.lock changes that required them. A reviewer can then see that the new dependency and its manifest edits belong together.
When you upgrade a plugin across a major version, reread its Changelog. Native requirements often change in major releases, such as new minimum SDKs or renamed permissions. Treat that as part of the upgrade, not as a surprise during testing.
Exercise
You add a camera plugin and run your app on an iOS simulator after only hot restarting. The Dart code calls the plugin and throws MissingPluginException. After a full rebuild, the app closes the first time the camera opens. Give the cause of each failure and the fix.
Show answer
The first failure happens because hot restart replaces only Dart code, so the plugin's native half was never built into the running app. Stop the app and run it again with flutter run, or run flutter clean first if the build state is stale. The second failure is most likely a missing usage description in ios/Runner/Info.plist. iOS terminates an app that requests the camera without an NSCameraUsageDescription string. Add that key with a short reason for using the camera, then rebuild.
Exercise
A teammate says: "I'll fix the Android build error by changing the plugin's files in the pub cache." Give two reasons this is a bad plan and say where the fix belongs.
Show answer
The pub cache is outside your project, so the edit is not in version control and teammates and CI never receive it. Pub also restores the package from its source, which overwrites the change on the next pub get or upgrade. The fix belongs in your project, such as android/app/build.gradle.kts for a minimum SDK or AndroidManifest.xml for a permission or merge conflict.