flutterdartflutter-local-notificationflutter-audioplayers

Flutter audioplayers 3.0.1 and flutter_local_notifications: ^13.0.0 help needed with new syntax


Hello i have made a flutter project and seems like pubspec packs are getting updated with new syntax every second with zero documentation of the replacements. i have some code, im basickly trying to create an alarm clock where on a notification i want a sound to play sounds pretty simple but i just cant for the life of me get it to work. I know it has something to do with my syntax being only aplicable to the old version of the audiplayers and onnotification some of the thing i could find on stackoverflow/google but other things is just impossible for me to figure out and it seems like im just going in circkles. I have ofcourse included the right version and packs in my pubspec.

here is all the code im having trouble with with the highlighted lines im getting my error.

for the onnotification im getting "the named parameter 'onSelectNotification' isn't defined. (Documentation) Try correcting the name to an existing named parameter's name, or defining a named parameter with the name 'onSelectNotification'.

for the audioplayer part im getting "This expression has a type of 'void' so its value can't be used. (Documentation) Try checking to see if you're using the correct API; there might be a function or call that returns void you didn't expect. Also check type parameters and variables which might also be void."

    Alarm(this.time, this.description, this.vibration) {
     initializeTimeZones(); // initialize the time zone database
     const androidInitializationSettings =
     AndroidInitializationSettings('launcher_icon');
     const DarwinInitializationSettings initializationSettingsIOS =
     DarwinInitializationSettings(
    onDidReceiveLocalNotification: onDidReceiveLocalNotification);
     const InitializationSettings initializationSettings =
     InitializationSettings(
    android: androidInitializationSettings,
    iOS: initializationSettingsIOS);
     flutterLocalNotificationsPlugin =           
    FlutterLocalNotificationsPlugin();
    flutterLocalNotificationsPlugin.initialize(initializationSettings,
    onSelectNotification: onSelectNotification);
   }


for the audio part 
 await playAudio();

  // Stop the audio after 30 seconds
  await Future.delayed(Duration(seconds: 30));
  await stopAudio();
}

Future<void> playAudio() async {
  AudioPlayer audioPlayer = AudioPlayer();
  String audioPath = "assets/audio/sound_alarm.mp3";

  // Load the audio file
  await audioPlayer.play(audioPath, isLocal: true);

  // Print a message when the audio file is playing
   print("Audio file is playing");
 }

 Future<void> stopAudio() async {
   AudioPlayer audioPlayer = AudioPlayer();
   await audioPlayer.stop();

   // Print a message when the audio file is stopped
   print("Audio file is stopped");
 }`
 ```

my sound can be found in both raw folder and assets>audio>sound_alarm.mp3


Solution

  • Every time a package updates with a new major version you can expect that there are breaking changes, if you want to stay on an old version you have to define that in your pubspec file, for example if you want to stay on 0.20.0 which is looks like you are on here you have to add ^0.20.0. This will ensure that you don't accidentally bump above 1.0.0.

    To your question:

    are getting updated with new syntax every second with zero documentation of the replacements

    It is actually documented, just follow the migration guide here. The problem you are having is that you are using strings directly instead of the new Source syntax, in this case you should use AssetSource.

    Here you can read about the changes you have to do to migrate to the new flutter_local_notifications version.