首页 > 解决方案 > 使用带有警报管理器回调的插件时出现 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)

标签: androidflutterdartflutter-plugindart-isolates

解决方案


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

推荐阅读