android - 使用带有警报管理器回调的插件时出现 MissingPluginException
问题描述
我在我的项目中使用android_alarm_manager_plus 2.0.2插件,在回调时我使用 A Function And I Play a Sound With assets_audio_player_3.0.6。
但是我遇到了这个错误
MissingPluginException(No implementation found for method stop on channel assets_audio_player)
MissingPluginException(No implementation found for method open on channel assets_audio_player)
[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: MissingPluginException(No implementation found for method open on channel assets_audio_player)
这个地方报告了这个错误但我找不到任何解决方案
在 Flutter 警报管理器回调中使用插件时出现 MissingPluginException
Flutter-AssetsAudioPlayer/issues/523
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 2.5.2, on Microsoft Windows [Version 10.0.19043.1237], locale en-US)
[!] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
! Some Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses
[√] Chrome - develop for the web
[√] Android Studio (version 4.2)
[√] Connected device (3 available)
此代码产生此错误
import 'package:android_alarm_manager_plus/android_alarm_manager_plus.dart';
import 'package:assets_audio_player/assets_audio_player.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void printHello() async{
final DateTime now = DateTime.now();
print("[$now] Hello, world! function='$printHello'");
///play audio
AssetsAudioPlayer assetsAudioPlayer =
AssetsAudioPlayer.withId("App_ID");
var audio = Audio(
"assets/audios/music.mp3",
);
assetsAudioPlayer.open(audio,
autoStart: true,
showNotification: true,
);
}
main() async {
WidgetsFlutterBinding.ensureInitialized();
await AndroidAlarmManager.initialize();
runApp(MyApp());
await AndroidAlarmManager.oneShotAt(DateTime.now().add(Duration(seconds: 15)), 123456789, printHello);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Welcome to Flutter',
home: Scaffold(
appBar: AppBar(
title: const Text('Welcome to Flutter'),
),
body: const Center(
child: Text('Hello World'),
),
),
);
}
}
结果
[2021-10-11 13:35:27.441400] Hello, world! function='Closure: () => void from Function 'printHello': static.'
MissingPluginException(No implementation found for method stop on channel assets_audio_player)
MissingPluginException(No implementation found for method open on channel assets_audio_player)
[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: MissingPluginException(No implementation found for method open on channel assets_audio_player)
解决方案
您需要将三个变量移动到 main 方法中。像这样的东西:
void printHello() async {
final DateTime now = DateTime.now();
print("[$now] Hello, world! function='$printHello'");
}
main() async {
WidgetsFlutterBinding.ensureInitialized();
AssetsAudioPlayer assetsAudioPlayer = AssetsAudioPlayer.withId('1234');
var audio = Audio(
"assets/audio/music.mp3",
);
await assetsAudioPlayer.open(
audio,
autoStart: true,
showNotification: true,
);
await AndroidAlarmManager.initialize();
runApp(MyApp());
await AndroidAlarmManager.oneShotAt(DateTime.now().add(Duration(seconds: 15)), 123456789, printHello);
}
class MyApp extends StatelessWidget {
...
}
推荐阅读
- php - 调用未定义的方法 DateTime::format 但 DateTime 正在工作
- java - Android - 如何使用 okhttp-retrofit 从保存的缓存中使特定 URL 无效/删除?
- c - 指定 RIDEV_NOLEGACY 时,Win32 原始输入阻止输入区域设置切换
- ruby - 使用gets时Ruby Loop无法中断
- c# - ASP.Net C# MCV - 将值从 Ajax Jquery 传递到控制器
- sql - 如何设置两个字段组合的输出参数,例如fullname=firstname+lastname?
- chatbot - 上一个 (%) 命令在 Rivescript 游乐场测试中引发错误?
- javascript - 如何在 OfficeJS 中显示类似于 Window.alert() 的消息
- angular - 如何使用不同的服务注入以角度创建可重用组件?
- r - 如何使用 data.table 对缺失值进行交叉制表