英文:
Flutter based Android app throwing MissingPluginException(No implementation found for method on channel)
问题
我正在尝试使我 Android 应用的原生端与 Flutter 进行通信。
这是在 Flutter 端的代码:
```dart
static const platform = const MethodChannel('getHallInfo');
@override
void initState() {
testing().then((String lst) {
setState(() {
if (lst != null) str = lst;
});
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[Text(str)],
);
}
static Future<String> testing() async {
String lst;
try {
lst = await platform.invokeMethod('getHalls');
} catch (e) {
print(e.toString());
}
return lst;
}
这是在原生 Java 端的代码:
private static final String CHANNEL = "getHallInfo";
super.configureFlutterEngine(getFlutterEngine());
GeneratedPluginRegistrant.registerWith(getFlutterEngine());
new MethodChannel(getFlutterEngine().getDartExecutor().getBinaryMessenger(), CHANNEL).setMethodCallHandler(new MethodChannel.MethodCallHandler() {
@Override
public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
if (methodCall.method.equals("getHalls")) {
String mm = "Succeeded";
System.out.println("Entered");
result.success(mm);
}
}
});
我的 Pubspec.yaml 文件内容:
name: flutterModule
description: A new flutter module project.
version: 1.0.0+1
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.3
flutter_svg: ^0.17.4
google_fonts: ^1.1.0
intl: ^0.16.1
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true
assets:
- assets/icons/
module:
androidX: true
androidPackage: com.example.flutterModule
iosBundleIdentifier: com.example.flutterModule
这是我遇到的错误:
MissingPluginException(No implementation found for method getHalls on channel getHallInfo)
注意: 终端上显示了 "Entered"
编辑: 我尝试过以下事项:
- 运行 flutter clean
- 运行 flutter upgrade
- 重启应用
但这个异常仍然存在。
<details>
<summary>英文:</summary>
I am trying to make the native side of my android app to communicate with Flutter.
This is on the Flutter side:
static const platform = const MethodChannel('getHallInfo');
@override
void initState() {
testing().then((String lst) {
setState(() {
if (lst != null) str = lst;
});
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[Text(str)],
);
}
static Future<String> testing() async {
String lst;
try {
lst = await platform.invokeMethod('getHalls');
} catch (e) {
print(e.toString());
}
return lst;
}
And this is on the native Java:
private static final String CHANNEL = "getHallInfo";
super.configureFlutterEngine(getFlutterEngine());
GeneratedPluginRegistrant.registerWith(getFlutterEngine());
new MethodChannel(getFlutterEngine().getDartExecutor().getBinaryMessenger(), CHANNEL).setMethodCallHandler(new MethodChannel.MethodCallHandler() {
@Override
public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
if (methodCall.method.equals("getHalls")) {
String mm= "Succeeded";
System.out.println("Entered");
result.success(mm);
}
}
});
My Pubspec.yaml:
name: flutterModule
description: A new flutter module project.
version: 1.0.0+1
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.3
flutter_svg: ^0.17.4
google_fonts: ^1.1.0
intl: ^0.16.1
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true
assets:
- assets/icons/
module:
androidX: true
androidPackage: com.example.flutterModule
iosBundleIdentifier: com.example.flutterModule
This is the error I get:
> MissingPluginException(No implementation found for method getHalls on channel getHallInfo)
**Note:** "Entered" is shown on the terminal
**Edit:** These are the things that I tried:
1. Run flutter clean
2. Run flutter upgrade
3. Restart the app
But this exception persists.
</details>
# 答案1
**得分**: 1
你只需要停止应用然后重新启动它。
确保在停止应用之前执行了 `pub get`。
使用以下命令:
```bash
flutter pub get
英文:
you just have to stop the app and restart it again.
make sure you did pub get
before you stop your app.
use the command .
flutter pub get
答案2
得分: 1
尝试运行 flutter clean
,
然后运行 flutter upgrade
,
接着运行 flutter run -v
。
如果这不起作用,请查看以下问题:
希望这能帮到您。
英文:
Try flutter clean
then flutter upgrade
then flutter run -v
If that doesn't work, have a look at the following issues:
issue01,
issue02
I hope this will help you
答案3
得分: 0
你只需要运行
Flutter clean
然后再重新启动/运行你的应用程序。
英文:
you just need to run
Flutter clean
then again restart/run your app
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论