asynchronous_method_channel 1.0.1+2
asynchronous_method_channel: ^1.0.1+2 copied to clipboard
The asynchronous method channel is a named channel which supports asynchronous return results for communicating with the Flutter application using asynchronous method calls.
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:asynchronous_method_channel/asynchronous_method_channel.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
static final platform =
AsynchronousMethodChannel('AsynchronousMethodChannelExample');
String _batteryLevel = 'Unknown';
String _timeInfo = "";
static const style = TextStyle(
fontSize: 16,
fontFamily: "monospace",
);
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String batteryLevel;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
final sb = StringBuffer();
final startAt = DateTime.now();
sb.writeln("[start] [$startAt]");
batteryLevel = await platform.invokeAsynchronousMethod("getBatteryLevel");
final endAt = DateTime.now();
sb.writeln("[end ] [$endAt]");
sb.writeln("[tag ] [hours:minutes:seconds:us]");
sb.writeln("[total] [${endAt.difference(startAt)}]");
_timeInfo = sb.toString();
} on PlatformException {
batteryLevel = '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(() {
_batteryLevel = batteryLevel;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('AsynchronousMethodChannel example app'),
),
body: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('Battery level: $_batteryLevel\n', style: style),
Text(_timeInfo, style: style),
Center(
child: FlatButton(
onPressed: initPlatformState,
child: Text("Get battery level"),
),
),
],
),
),
),
);
}
}
copied to clipboard