kat_device 0.0.2
kat_device: ^0.0.2 copied to clipboard
Get info of device as android version, gsf, android id.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:kat_device/kat_device.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
String _gsf = 'Unknown';
String _androidId = 'Unknown';
String _carrierName = 'Unknown';
String _macAddress = 'Unknown';
final _katDevicePlugin = KatDevice();
@override
void initState() {
super.initState();
initPlatformState();
initGsfState();
initAndroidIdState();
initCarrierNameState();
initMacAddressState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
platformVersion = await _katDevicePlugin.getAndroidVersion() ??
'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initGsfState() async {
String gsf;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
gsf = await _katDevicePlugin.getGsf() ?? 'Unknown gsf';
} catch (e) {
gsf = '$e';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_gsf = gsf;
});
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initAndroidIdState() async {
String androidId;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
androidId = await _katDevicePlugin.getAndroidId() ?? 'Unknown android id';
} catch (e) {
androidId = '$e';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_androidId = androidId;
});
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initCarrierNameState() async {
String carrierName;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
carrierName =
await _katDevicePlugin.getCarrierName() ?? 'Unknown carrier name';
} catch (e) {
carrierName = '$e';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_carrierName = carrierName;
});
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initMacAddressState() async {
String macAddress;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
macAddress =
await _katDevicePlugin.getMacAddress() ?? 'Unknown mac address';
} catch (e) {
macAddress = '$e';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return;
setState(() {
_macAddress = macAddress;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Running on: Android $_platformVersion\n'),
Text('Gsf: $_gsf\n'),
Text('Android Id: $_androidId\n'),
Text('Carrier Name: $_carrierName\n'),
Text('Mac Address: $_macAddress\n'),
],
),
),
),
);
}
}