【问题标题】:MissingPluginException when using a plugins With Alarm Manager Call Back使用带有警报管理器回调的插件时出现 MissingPluginException
【发布时间】:2021-11-29 23:45:30
【问题描述】:

我在我的项目中使用android_alarm_manager_plus 2.0.2 插件,在回调时我使用一个函数并使用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)

这个地方报告了这个错误但我找不到任何解决方案

MissingPluginException When Using Plugins in Flutter Alarm Manager Callbacks

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)

【问题讨论】:

  • 尝试flutter clean然后再次运行,或者你可以运行它会再次下载你所有的包flutter pub cache repair它会解决你的问题
  • 从 android 文件夹尝试 gradlew clean 和 gradlew build。我认为您需要先同步或构建 android 项目。然后在flutter项目中再试一次。
  • 问题解决了吗?
  • 嘿@mohandesR。我刚刚在颤振医生中看到一些 android 许可证不被接受。只需运行flutter doctor --android-licenses 和许可证。如果这没有帮助,请告诉我。
  • @ch271828n 不,我还找不到任何解决方案。

标签: android flutter dart flutter-plugin dart-isolates


【解决方案1】:

您需要将这三个变量移动到 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 {
...
}

【讨论】:

  • 感谢您的回答。但我需要在特定时间播放声音。 (在后台)。警报之类的东西。例如 4 小时后。不是马上。
猜你喜欢
  • 2020-12-19
  • 1970-01-01
  • 2018-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多