我正在尝试在Flutter项目中开发特定的平台代码。 我使用flutter网站中的步骤,并编写了一种方法来测试功能。
Java代码:
MustacheResourceTemplateLoader loader = new MustacheResourceTemplateLoader("emailTemplates/", ".html"); // Inside resources emailTemplates dir
Reader reader = loader.getTemplate("index");
Template tmpl = Mustache.compiler().compile(reader);
// Template String "One, two, {{three}}. Three sir!"
Map<String, String> data = new HashMap<String, String>();
data.put("three", "six");
Dart代码:
import android.os.Bundle;
import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "hyperpay";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
new MethodCallHandler() {
@Override
public void onMethodCall(MethodCall call, Result result) {
// Note: this method is invoked on the main thread.
if (call.method.equals("getPlatformVersion")) {
result.success("Android " + android.os.Build.VERSION.RELEASE);
} else {
result.notImplemented();
}
}
}
);
}
}
在IOS中,我没有问题,代码已成功运行。 但是在Android中,我不断收到以下异常:
Future<void> setPlatformVersionState() async {
String platformVersion;
const channel = MethodChannel('hyperpay');
try {
final platformVersion = await channel.invokeMethod('getPlatformVersion');
setState(() {
_platformVersion = platformVersion;
});
} on PlatformException catch (error) {
setState(() {
_platformVersion = null;
});
}
}
答案 0 :(得分:0)
我解决了这个问题。
Java代码应位于:
android/app/src/main/java/com/**example**/app_name/MainActivity.java
但是我已经写在:
android/app/src/main/java/com/**username**/app_name/MainActivity.java
因此,在调用invokemethod时,在MainActivity.java中找不到任何通道/方法