asynchronous_method_channel 1.0.1+2

  • Readme
  • Changelog
  • Example
  • Installing
  • 66

asynchronous_method_channel #

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

Flutter Environment #

  • version: v1.9.1+hotfix.6
  • channel: master

Use AsynchronousMethodChannel on Android by kotlin #

The following is an example of using the kotlin coroutine for asynchronous tasks and returning results.

One thing you need to know before you officially start is that the gradle of the Android module in the Flutter app does not automatically import the packages we need, you must manually add the following code.

import io.flutter.plugins.asynchronous_method_channel.AsynchronousMethodChannel

class MainActivity: FlutterActivity() , AsynchronousMethodChannel.MethodCallHandler {
    companion object{
        const val CHANNEL="AsynchronousMethodChannelExample"
    }
    private var parentJob = Job()
    private val coroutineContext: CoroutineContext
        get() = parentJob + Dispatchers.Main
    private val scope = CoroutineScope(coroutineContext)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        GeneratedPluginRegistrant.registerWith(this)
        AsynchronousMethodChannel(flutterView, CHANNEL).setMethodCallHandler(this)
    }


    override fun onMethodCall(call: MethodCall, result: AsynchronousMethodChannel.Result) {
        when (call.method) {
            "getBatteryLevel" -> {
                result.success(null)
                scope.launch(Dispatchers.IO){
                    // Do something
                    // Perform asynchronous time-consuming tasks

                    // Just return results after 2 seconds
                    delay(2000)

                    // The method in AsynchronousMethodChannel.Result must be called on the main thread of the platform
                    scope.launch(Dispatchers.Main){
                        result.successAsynchronous(getBatteryLevel().toString())
                    }
                }
            }
            else -> result.notImplemented()
        }
    }

    private fun getBatteryLevel(): Int {
        val batteryLevel: Int
        batteryLevel = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val batteryManager = getSystemService(Context.BATTERY_SERVICE) as BatteryManager
            batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)
        } else {
            val intent = ContextWrapper(applicationContext).registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
            intent!!.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) * 100 / intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
        }
        return batteryLevel
    }

    override fun onDestroy() {
        // cancel all asynchronous jobs
        scope.cancel()
        super.onDestroy()
    }
}

Use AsynchronousMethodChannel on Flutter by dart #

The following is is an example of using AsynchronousMethodChannel in a Flutter application.


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"),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Screenshots #

Use AsynchronousMethodChannel in tests #


void main() {
  TestWidgetsFlutterBinding.ensureInitialized();

  final AsynchronousMethodChannel channel =
      AsynchronousMethodChannel('asynchronous_method_channel');

  setUp(() {
    channel.setMockAsynchronousMethodCallHandler(
        (MethodCall methodCall, MockResult result) async {
      switch (methodCall.method) {
        case "asynchronousMethod":
          // Delay 30 milliseconds to return results
          Future.delayed(Duration(milliseconds: 30),
              () => result.success(methodCall.arguments));
          break;
        case "syncMethod":
          return "ok";
          break;
        case "getBatteryLevel":
          result.success("100");
          break;
      }
      return null;
    });
  });

  tearDown(() {
    channel.setMockAsynchronousMethodCallHandler(null);
  });

  test('testMethod', () async {
    expect(
      await channel.invokeAsynchronousMethod(
        "asynchronousMethod",
        {"arg": "arg1"},
      ),
      {"arg": "arg1"},
    );
    expect(
      await channel.invokeMethod("syncMethod"),
      "ok",
    );
    expect(
      await channel.invokeAsynchronousMethod("getBatteryLevel"),
      "100",
    );
  });
}

Use AsynchronousMethodChannel on IOS #

Release next version.

中文文档 #

More #

Please see example.

1.0.1+2 #

  • Update example and README.md.

1.0.1+1 #

  • Add a link to a Simplified Chinese document.

1.0.1 #

  • Use state error instead of exception.
  • No more catch errors when handling mock method calls.
  • Update description.

1.0.0 #

  • Release 1.0.0 .

example/README.md

asynchronous_method_channel_example #

Demonstrates how to use the asynchronous_method_channel plugin.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  asynchronous_method_channel: ^1.0.1+2

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:asynchronous_method_channel/asynchronous_method_channel.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
32
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
66
Learn more about scoring.

We analyzed this package on Jan 19, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4
  • Flutter: 1.12.13+hotfix.5

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test