Reputation: 996
I have succeeded in calling a flutter method in Java using 'MethodChannel'.
Java Code
if(call.method.equals("drawFont")){
int res = callFlutterMethod(0xF020);
System.out.println("drawFont Result : " + res);
}
///Call Flutter Method
public static void callFlutterMethod(int nFontData){
channel.invokeMethod("drawFont", nFontData);
System.out.println("pass FontData!!");
}
Flutter Code
onListener(){
platform.setMethodCallHandler((call) async{
if(call.method == 'drawFont'){
print('call java drawFont : ${call.arguments}');
return Text(String.fromCharCode(call.arguments));
}
});
return Text('Listener Failed');
}
The problem is that whenever I call the flutter method with methodChannel
in java, I want the widget to be created. If there is a way, please give me a simple example code. Thank you.
Example
Column(
children: [
//call Widget Text
],
)
Upvotes: 0
Views: 40
Reputation: 1583
For such case you have to use FutureBuilder
Your Java code should be looks like
@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
new MethodChannel(flutterEngine.getDartExecutor(), YOUR_CHANNEL_NAME).setMethodCallHandler(
new MethodCallHandler() {
@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("drawFont")) {
result.success(0xF020);
}
}
);
}
Your Flutter code
Column(
children: [
FutureBuilder<String>(
future: platform.invokeMethod('drawFont'),
builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
if (snapshot.hasData) {
return Text(String.fromCharCode(snapshot.data));
} else if (snapshot.hasError) {
return Text('Error');
} else {
return Text('Loading');
}
}
),
],
);
Upvotes: 1