I am trying to integrate the flutter module in the native code of android. I created the flutter module and create aar file from the module. I successfully imported the module and able to run it. But I am trying to do is to pass parameters from the android native code to the flutter module like this:
public class MainActivity extends FlutterActivity {
@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
super.configureFlutterEngine(flutterEngine);
MethodChannel channel = new MethodChannel(
flutterEngine.getDartExecutor().getBinaryMessenger(), "hello"
);
channel.invokeMethod("ok", "done");
}
}
My flutter module:
void main() => runApp(MyHomePage(title: "Testing",));
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
String nativeMessage = '';
MethodChannel _methodChannel = new MethodChannel("hello");
@override
void initState() {
super.initState();
_methodChannel.setMethodCallHandler((call) => _incrementCounter(call));
}
Future<dynamic> _incrementCounter(MethodCall call) {
print("FaaiqKhan flutter");
print("-----------------------------------------------");
print(call.method);
print(call.arguments);
print("-----------------------------------------------");
return null;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Testing",
home: Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),// This trailing comma makes auto-formatting nicer for build methods.
),
);
}
}
when I run my android application it always open the flutter application page but didn't print any thing. Did I miss something or I done something wrong.
question from:
https://stackoverflow.com/questions/65884935/how-to-pass-data-from-android-native-code-to-flutter-module 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…