You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行Flutter木琴项目时遇AudioCache的byteData未定义错误

解决Angela Yu Flutter木琴项目中audioplayers的byteData错误

问题场景

运行Angela Yu博士第9节木琴Flutter项目时,触发如下错误:

C:/Users/DELL/AppData/Local/Pub/Cache/hosted/pub.dev/audioplayers-4.1.0/lib/src/audio_cache.dart:92:29: Error: The getter 'byteData' isn't defined for the class 'AudioCache'.
'AudioCache' is from 'package:audioplayers/src/audio_cache.dart' ('/C:/Users/DELL/AppData/Local/Pub/Cache/hosted/pub.dev/audioplayers-4.1.0/lib/src/audio_cache.dart').audio_cache.dart:1
Try correcting the name to the name of an existing getter, or defining a getter or field named 'byteData'.
await file.writeAsBytes(byteData.buffer.asUint8List());
^^^^^^^^

错误原因

audioplayers 4.x版本彻底移除了AudioCache类的旧API(包括byteData属性),而课程代码基于旧版本audioplayers编写,导致版本不兼容。

解决方案

方案1:降级到兼容的旧版本

在项目的pubspec.yaml文件中,将audioplayers的版本指定为^0.20.1:

dependencies:
  flutter:
    sdk: flutter
  audioplayers: ^0.20.1

保存后执行命令 flutter pub get 重新拉取依赖,即可直接使用课程中的原有代码。

方案2:适配新版本API

如果不想降级,改用audioplayers 4.x的新写法替换原有代码:

  1. 导入新版包:
import 'package:audioplayers/audioplayers.dart';
  1. 替换AudioCache的播放逻辑,比如原来的AudioCache().play('note1.wav')改为:
final player = AudioPlayer();
await player.play(AssetSource('note1.wav'));
  1. 确保pubspec.yaml中正确配置了assets路径:
assets:
  - assets/note1.wav
  - assets/note2.wav
  # 按需添加其他木琴音频文件

内容的提问来源于stack exchange,提问作者RAGEMAX 786a

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 09:57:17