D.8. How do I interact with the device and use native APIs?

Perhaps the biggest difference between native mobile development and Flutter development is that Flutter doesn’t have direct access to the devices underlying SDK. Your Flutter app actually is hosted in a ViewController on iOS, but you can’t communicate with it directly.

Flutter solved this problem by creating platform channels, which can communicate with the ViewController. According to the docs, “Platform channels are essentially an asynchronous messaging mechanism that bridges 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.”

Most platform channel work would be included in your app as a plugin that encapsulates the native code and the Dart code, and exposes the Dart API. You can write your own plugins to do this, which basically consists of writing iOS native code, because the Dart code is generally quite simple. This isn’t necessary, though, because there are already a ton of plugins provided by the Pub package manager that communicate with native APIs. These are some examples:

  • image_picker to access the camera
  • geolocator to access the GPS sensor
Note

See the Pub package manager appendix to learn more.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.183.138