`package com.testmrthodcn;
import androidx.annotation.NonNull;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;
public class MainActivity extends FlutterActivity {
public static final String CHANNEL = "com.itcat.humanos/sasigarn";
@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine){
super.configureFlutterEngine(flutterEngine);
System.out.println("flutter print :: configureFlutterEngine222");
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
.setMethodCallHandler((call, result) -> {
System.out.println("flutter print :: result");
if (call.method.equals("getNativeData")) {
System.out.println("flutter print :: if configureFlutterEngine");
String data = "55555555 from native"; // ทำงานกับข้อมูลจาก Android
result.success(data);
} else {
System.out.println("flutter print :: else configureFlutterEngine");
result.notImplemented();
}
}
);
System.out.println("flutter print :: after MethodChannel");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this.getFlutterEngine());
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(
FlutterActivity
.withNewEngine()
.initialRoute("/test_methodchannel")
.build(MainActivity.this)
);
}
});
}
}`
`import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class TestMethodchannel extends StatefulWidget {
@override
_TestMethodchannel createState() => _TestMethodchannel();
}
class _TestMethodchannel extends State<TestMethodchannel> {
static const channel = const MethodChannel('com.itcat.humanos/sasigarn');
String _dataFromNative = 'No Data';
Future<void> _getNativeData() async {
String? data;
try {
final String? result = await channel.invokeMethod<String>('getNativeData', {'arg1': 'value1', 'arg2': 'value2'});
data = result;
} on PlatformException catch (e) {
data = "Failed to get data: '${e.message}'.";
}
print('flutter data : '+data.toString());
setState(() {
_dataFromNative = data.toString();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Native Data Fetcher'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: _getNativeData,
child: Text('Fetch Data from Native'),
),
SizedBox(height: 20),
Text('Data from Native: $_dataFromNative'),
],
),
),
);
}
}`
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: MissingPluginException(No implementation found for method getNativeData on channel com.itcat.humanos/sasigarn) #0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:308:7)
I've tried almost every code mod. But still unable to send value. I tried cleaning flutter and it still doesn't work.