Sub topic 9
Plugins That Need Android, iOS, or Web Setup
Assume you know basic Dart, have a Flutter project, can run terminal commands, and understand the main sections of pubspec.yaml. This chapter focuses on Flutter plugins that call native Android, iOS, or browser APIs.
Why Plugins Need Platform Setup
A Dart package contains code that runs in Dart. A plugin also contains platform implementations. For example, a location plugin can call Android location services, iOS Core Location, or the browser's geolocation API.
Flutter connects the Dart API to the platform implementation through generated plugin registration. Adding the dependency usually enables this connection, but the operating system still requires permission declarations and configuration.
A Worked Example with Geolocation
Add the plugin from the project directory:
flutter pub add geolocatorThe plugin's Dart API is the same across platforms, but each platform has different setup requirements. This example requests permission before reading a location.
import 'package:geolocator/geolocator.dart';
Future<Position> getCurrentPosition() async {
final serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
throw Exception('Location services are disabled.');
}
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();
}The application must still explain why it needs location access. A runtime request alone does not replace the platform declaration.
| Platform | Typical configuration | Common failure |
|---|---|---|
| Android | Add permissions to `AndroidManifest.xml` and meet the plugin's SDK requirements | Permission is missing or denied |
| iOS | Add usage descriptions to `Info.plist` and check deployment requirements | The app is rejected or the request fails |
| Web | Use a supported browser context such as HTTPS or localhost | Browser blocks the request |
Android
Location plugins commonly require permissions in android/app/src/main/AndroidManifest.xml. Use the exact permissions listed in the plugin documentation. Some capabilities also require a foreground service, background permission, or a particular Android SDK level.
After changing Android configuration, rebuild the application. Hot reload does not reload every native setting.
iOS
iOS usage descriptions belong in ios/Runner/Info.plist. Add the keys required by the plugin, such as a location usage description. The text is shown in the system permission dialog.
Some plugins also require an iOS deployment target, entitlements, or changes to AppDelegate. Copy the plugin's documented configuration rather than guessing key names.
Web
Web plugins use browser APIs and browser security rules. Test geolocation through localhost during development and HTTPS in deployed environments. The browser controls the permission prompt, so the Flutter app cannot grant access silently.
Troubleshooting Plugin Setup
Check these items when a plugin works on one target but fails on another:
- Confirm the platform is supported by the package's pub.dev page.
- Compare your Flutter, Android, iOS, and plugin versions with the package requirements.
- Verify manifest,
Info.plist, entitlement, and web settings. - Stop and rebuild after native configuration changes.
- Check the device or browser logs for the missing permission or unsupported API.
- Test denied and permanently denied permissions, not only the successful path.
Exercise
Why can a plugin compile successfully but still fail when its API runs?
Show answer
Compilation confirms that the Dart package is available, but runtime access may still require platform permissions, native configuration, a supported device, or browser security conditions.