Interacting with hardware, third party services and the platform
How do I interact with the platform, and with platform native code?
Flutter doesn’t run code directly on the underlying platform; rather, the Dart code that makes up a Flutter app is run natively on the device, “sidestepping” the SDK provided by the platform. That means, for example, when you perform a network request in Dart, it runs directly in the Dart context. You don’t use the Android or iOS APIs you normally take advantage of when writing native apps. Your Flutter app is still hosted in a native app’s
ViewController as a view, but you don’t have direct access to the
ViewController itself, or the native framework.
This doesn’t mean Flutter apps cannot interact with those native APIs, or with any native code you have. Flutter provides platform channels, that communicate and exchange data with the
ViewController that hosts your Flutter view. Platform channels are essentially an asynchronous messaging mechanism that bridge the Dart code with the host
ViewController and the iOS framework it runs on. You can use platform channels to execute a method on the native side, or to retrieve some data from the device’s sensors, for example.
In addition to directly using platform channels, you can use a variety of pre-made plugins that encapsulate the native and Dart code for a specific goal. For example, you can use a plugin to access the camera roll and the device camera directly from Flutter, without having to write your own integration. Plugins are found on the pub.dev site, Dart and Flutter’s open source package repository. Some packages might support native integrations on iOS, or Android, or both.
How do I access the GPS sensor?
geolocator community plugin.
How do I access the camera?
image_picker plugin is popular for accessing the camera.
How do I log in with Facebook?
To log in with Facebook, use the
flutter_facebook_login community plugin.
How do I use Firebase features?
firebase_admobfor Firebase AdMob
firebase_analyticsfor Firebase Analytics
firebase_authfor Firebase Auth
firebase_corefor Firebase’s Core package
firebase_databasefor Firebase RTDB
firebase_storagefor Firebase Cloud Storage
firebase_messagingfor Firebase Messaging (FCM)
cloud_firestorefor Firebase Cloud Firestore
You can also find some third-party Firebase plugins on pub.dev that cover areas not directly covered by the first-party plugins.
How do I build my own custom native integrations?
Flutter’s plugin architecture, in a nutshell, is much like using an Event bus in Android: you fire off a message and let the receiver process and emit a result back to you. In this case, the receiver is code running on the native side on Android or iOS.