Method Channel Sent Value In Flutter :

MainActivity.kt
package com.jdkgroup.flutter_tutorial import android.os.Bundle import io.flutter.app.FlutterActivity import io.flutter.plugin.common.MethodChannel import io.flutter.plugins.GeneratedPluginRegistrant class MainActivity : FlutterActivity() { private val channel = "method_channel_plugin" private var arguments = HashMap<String, Object>() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) GeneratedPluginRegistrant.registerWith(this) MethodChannel(flutterView, channel).setMethodCallHandler { methodCall, result -> if (methodCall.method == "callName") { arguments = methodCall!!.arguments as (HashMap<String, Object>) result.success(arguments.toString()) } if (methodCall.method == "callTitle") { result.success("Flutter Tutorial") } } } }
main.dart
import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Tutorial', theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(), ); } } class MyHomePage extends StatefulWidget { @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { final platform = MethodChannel("method_channel_plugin"); String valueStore; final StreamController<String> _streamController = StreamController<String>(); @override void initState() { getData(); super.initState(); } Future getData() async { var value; Map<String, dynamic> options = new Map(); options.putIfAbsent("name", () => "kamlesh"); try { value = await platform.invokeMethod("callName", options); _streamController.add(value); } catch (e) {} return value; } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("Method Channel Sent Value in Flutter"), ), body: StreamBuilder<String>( stream: _streamController.stream, builder: (BuildContext context, AsyncSnapshot<String> snapshot) { if (!snapshot.hasData) { return Container(); } return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Text(snapshot.data), ])); })); } }
The flutter tutorial is a website that bring you the latest and amazing resources of code. All the languages codes are included in this website. The languages like flutter, android, java,kotlin etc.with the help of this languages any user can develop the beautiful application
For more information about Flutter. visit www.fluttertutorial.in