To access native device features in Flutter, you'll typically use platform channels to communicate between your Flutter code (Dart) and the native code (Android/iOS). Flutter provides a way to invoke methods on the platform side and receive results back.
Here’s a general outline for accessing native device features:
Flutter uses platform channels to send messages from Dart to native code and vice versa.
In your Flutter app, create a method channel to communicate with the native side.
import 'package:flutter/services.dart';
class NativeBridge {
static const platform = MethodChannel('com.example.device_features');
Future
getDeviceInfo() async { try {
final String result = await platform.invokeMethod('getDeviceInfo');
return result;
} on PlatformException catch (e) {
return "Failed to get device info: '${e.message}'.";
}
}
}
In the code above:
MethodChannel
is created with a unique name (com.example.device_features
).invokeMethod
sends the message to the native side.For Android, you need to modify the MainActivity to handle the method channel.
In MainActivity.java
(Java) or MainActivity.kt
(Kotlin), implement the handler for the platform channel:
Java:
import android.os.Bundle;
import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.MethodChannel;
public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "com.example.device_features";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new MethodChannel(getFlutterEngine().getDartExecutor(), CHANNEL)
.setMethodCallHandler(
(call, result) -> {
if (call.method.equals("getDeviceInfo")) {
String deviceInfo = "Android Device: " + android.os.Build.MODEL;
result.success(deviceInfo);
} else {
result.notImplemented();
}
}
);
}
}
For iOS, you need to modify the AppDelegate
to handle the method channel.
In AppDelegate.swift
, add the following code:
import UIKit
import Flutter
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
private let CHANNEL = "com.example.device_features"
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
let controller = window.rootViewController as! FlutterViewController
let methodChannel = FlutterMethodChannel(name: CHANNEL, binaryMessenger: controller.binaryMessenger)
methodChannel.setMethodCallHandler { (call, result) in
if call.method == "getDeviceInfo" {
let deviceInfo = "iOS Device: \(UIDevice.current.model)"
result(deviceInfo)
} else {
result(FlutterMethodNotImplemented)
}
}
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
Now, you can use the NativeBridge
class to call the native code and get the device info:
void fetchDeviceInfo() async {
NativeBridge nativeBridge = NativeBridge();
String deviceInfo = await nativeBridge.getDeviceInfo();
print(deviceInfo);
}
You can access many native features this way, such as:
camera
or image_picker
.location
or geolocator
.contacts_service
.path_provider
or shared_preferences
.flutter_blue
.If you want to access more advanced native features, you may need to create more complex platform channels or use existing Flutter packages for those features. Flutter's official documentation has a section on platform channels that goes into further detail.
Let me know if you need help with a specific device feature!
👆👆Build high performance app
👆👆Build scalable app