我目前正在开发一款Pomodoro计时器应用程序,使用Ffltter和Riverpod进行状态管理.当我try 从设置页面更新默认计时器值时,遇到了一个问题.

以下是我当前的功能:

我有一个Pomodoro定时器,缺省值为3.定时器结束时会发出闹钟声. 当用户更新计时器值时,更新的计时器工作,但一旦计时器结束,就不会播放alert 声. 我的初始Pomodoro定时器状态提供程序声明如下:最终PomodoroTimerProvider=StateProvider((Ref)=>3);

我认为问题出在这条线上,因为似乎只有当计时器值为3时才有条件地播放alert 声.

以下是我想要实现的目标:

用户应该能够更新计时器值. 与默认定时器一样,更新后的定时器应在结束时触发alert 声音. 用户还应该能够从一组选项中 Select alert 声音. 以下是我的代码的相关部分:

// timer_provider.dart

import 'dart:async';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:just_audio/just_audio.dart';
import 'package:pomoworkojunio2023/domain/entities/sound.dart';

final pomodoroTimerProvider = StateProvider<int>((ref) => 3);
// ... Rest of the code

class TimerNotifier extends StateNotifier<int> {
  // ...

  Future<void> playSound() async {
    if (!_isDisposed) {
      final selectedSound = ref.watch(selectedSoundProvider.notifier).state;
      if (_audioPlayer.processingState != ProcessingState.idle) {
        await _audioPlayer.stop();
      }
      await _audioPlayer.setAsset(selectedSound.path);
      await _audioPlayer.load();
      _audioPlayer.play();
    }
  }
  
  void updateDuration(int newDuration) {
    // ...
  }
}

// settings_page.dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../domain/entities/timer_provider.dart';

class SettingsPage extends ConsumerWidget {
  // ...

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return Scaffold(
      // ...
      body: ListView(
        // ...
        children: <Widget>[
          // ...
          ElevatedButton(
            onPressed: () {
              // ...
              ref.read(timerNotifierProvider.notifier).updateDuration(0);
            },
            child: const Text('Save'),
          )
        ],
      ),
    );
  }
}

class TimerSettingState extends ConsumerStatefulWidget {
  // ...
}

class _TimerSettingStateState extends ConsumerState<TimerSettingState> {
  // ...
}

我如何修改我的代码,使alert 声也为更新的计时器值播放?任何循序渐进的指南都将不胜感激!

注意:我使用的是Riverpod v2.3.6和Just_Audio v0.9.34.有关这些产品包的更多信息,请参阅Riverpod documentationjust_audio package on pub.dev.

我还在GitHub上上传了完整的代码片段供参考: https://github.com/codemotozu/stack-overflow-question-riverpod

我非常感谢您提供的任何指导或建议,以帮助我解决这个问题.非常感谢您的时间和专业知识.我期待着您的回复.

推荐答案

您的错误可能是您在回调中使用了ref.watch.只需使用ref.read:

Future<void> playSound() async {
    if (!_isDisposed) { //     ⬇here⬇
      final selectedSound = ref.read(selectedSoundProvider.notifier).state;
      if (_audioPlayer.processingState != ProcessingState.idle) {
        await _audioPlayer.stop();
      }
      await _audioPlayer.setAsset(selectedSound.path);
      await _audioPlayer.load();
      _audioPlayer.play();
    }
  }

Flutter相关问答推荐

Flutter日期和时间 Select 器未显示

滚动时应用栏 colored颜色 更改Flutter

创建多个具有适当填充和对齐的按钮的最佳方法

相同的Flutter 代码库,但Firebase登录适用于Web应用程序,但无法登录iOS应用程序

如何在Flutter 中不从getX库中初始化GetxController中的变量

带有图标抖动的自定义下拉按钮

SliverAppbar 呈蓝色

Flutter记录返回类型问题

Flutter:我应该使用哪种应用内购买服务?

在flutter中从图库或相机中 Select 图像的方法

如何将行推到列的底部

这是使用导航功能处理从一页到另一页的移动的最佳方式

如何使用 riverpod_annotation 创建非自动处置提供程序?

如何为文本主题设置多种 colored颜色 ?

type '({bool growable}) => List' 不是类型转换中类型 'List' 的子类型

如何判断 text.contains() 是否包含多个值

如何在 Flutter 中的页面加载上的 Listview 上应用自动滚动

Flutter - 检测手指何时进入容器

将容器扩展到安全区域 - Flutter

使用 hydrad_bloc 的预览包