Updated 28 April 2023
Flutter provides a flexible system to access platform-specific features of Android or IOS.
We can access platform-specific functionality like device information, sensors, battery, device camera, battery level, open browser, etc using flutter platform channels.
Read more about Flutter app development from mobikul.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package com.example.flutter_app import android.util.Log import androidx.annotation.NonNull import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.engine.FlutterEngine import io.flutter.plugin.common.MethodChannel class MainActivity : FlutterActivity() { private val CHANNEL = "com.example.flutter/device_info" // Unique Channel override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) { super.configureFlutterEngine(flutterEngine) MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { // Note: this method is invoked on the main thread. call, result -> if (call.method == "getDeviceInfo") { val deviceInfo: HashMap<String, String> = getDeviceInfo() if (deviceInfo.isNotEmpty()) { result.success(deviceInfo) } else { result.error("UNAVAILABLE", "Device info not available.", null) } } else { result.notImplemented() } } } private fun getDeviceInfo(): HashMap<String, String> { val deviceInfo = HashMap<String, String>() deviceInfo["version"] = System.getProperty("os.version").toString() // OS version deviceInfo["device"] = android.os.Build.DEVICE // Device deviceInfo["model"] = android.os.Build.MODEL // Model deviceInfo["product"] = android.os.Build.PRODUCT // Product return deviceInfo } } |
Let’s declare platform Channel
1 2 |
static const platform = const MethodChannel('com.example.flutter/device_info'); |
Invoke function by passing the function name and arguments.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Future<void> _getDeviceInfo() async { String result; try { platform.invokeMethod('getDeviceInfo',"Pass your payload supported by StandardMethodCodec").then((value) { result = value.toString(); setState(() { deviceInfo = result; }); }); } on PlatformException catch (e) { print("_getDeviceInfo==>${e.message}"); } } |
Let’s Review Complete Flutter code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, visualDensity: VisualDensity.adaptivePlatformDensity, ), home: MyHomePage(title: 'Flutter Demo Home Page'), ); } } class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); final String title; @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { static const platform = const MethodChannel('com.example.flutter/device_info'); String deviceInfo = ""; @override void initState() { _getDeviceInfo(); super.initState(); } Future<void> _getDeviceInfo() async { String result; try { platform.invokeMethod('getDeviceInfo').then((value) { result = value.toString(); setState(() { deviceInfo = result; }); }); } on PlatformException catch (e) { print("_getDeviceInfo==>${e.message}"); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Center(child: Text(deviceInfo)), ); } } |
I hope this blog will help to access the platform-specific APIs and features.
Happy Learning 🙂
Reference Links :
https://flutter.dev/docs/development/platform-integration/platform-channels
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.