asynchronous_method_channel 1.0.1+2 copy "asynchronous_method_channel: ^1.0.1+2" to clipboard
asynchronous_method_channel: ^1.0.1+2 copied to clipboard

discontinued

The asynchronous method channel is a named channel which supports asynchronous return results for communicating with the Flutter application using asynchronous method calls.

example/lib/main.dart

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"),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
2
likes
40
pub points
0%
popularity

Publisher

unverified uploader

The asynchronous method channel is a named channel which supports asynchronous return results for communicating with the Flutter application using asynchronous method calls.

Repository (GitHub)
View/report issues

License

BSD-2-Clause (LICENSE)

Dependencies

flutter

More

Packages that depend on asynchronous_method_channel