Skip to content

Instantly share code, notes, and snippets.

@neiljaywarner
Created March 29, 2019 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neiljaywarner/15809d1b83e45e003cd23ed7a3fe3999 to your computer and use it in GitHub Desktop.
Save neiljaywarner/15809d1b83e45e003cd23ed7a3fe3999 to your computer and use it in GitHub Desktop.
Platform call and device_info simple plugin usage
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:device_info/device_info.dart';
import 'package:flutter/services.dart';
void main() async {
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
if (Platform.isAndroid) {
AndroidDeviceInfo androidInfo = await (deviceInfo.androidInfo);
print('Running on ${androidInfo.model}'); // e.g. "Moto G (4)"
}
if (Platform.isIOS) {
IosDeviceInfo iosInfo = await (deviceInfo.iosInfo);
print('Running on ${iosInfo.utsname.machine}'); // e.g. "iPod7,1"
}
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) => MaterialApp(title: 'Flutter Demo', theme: ThemeData(primarySwatch: Colors.blue), home: MyHomePage());
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
static const platform = const MethodChannel('com.example.plugin');
String _platformString = 'Unknown string';
Future<void> _getPlatformString() async {
String platformString;
try {
final String result = await platform.invokeMethod('plugin_hi');
platformString = 'string from platform= $result';
} on PlatformException catch (e) {
platformString = "Failed to get platform string '${e.message}'.";
}
setState( () => _platformString = platformString);
}
@override
initState() {
super.initState();
_getPlatformString();
}
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: Text("Demo: Log model name in log, platform call result in text")),
body: Center(child: Text(_platformString)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment