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

使用audiplayers播放Firestore音乐URL报错:String并非Source子类型

解决audiplayers播放URL时的类型错误:'String' is not a subtype of type 'Source'

这个错误的核心原因是audiplayers库的play()方法要求传入Source类型的参数,但你直接传入了Firestore获取到的字符串格式的音乐URL,导致类型不匹配。

解决方法很简单,只需要把字符串URL包装成UrlSource对象即可,UrlSource是audiplayers提供的专门用于网络音频源的类型。

修改后的代码如下:

Row(
  mainAxisAlignment: MainAxisAlignment.spaceBetween,
  children: [
    Text(formatTime(position)),
    Text(formatTime(duration - position)),
    CircleAvatar(
      radius: 35,
      child: IconButton(
        icon: Icon(isPlaying ? Icons.pause : Icons.play_arrow_rounded),
        onPressed: () async {
          if (isPlaying) {
            await audioPlayer.pause();
          } else {
            // 将字符串URL包装为UrlSource类型
            await audioPlayer.play(UrlSource(widget.music));
          }
        },
      ),
    )
  ],
)

如果你的项目里还没导入对应的类,确保在文件顶部的import语句包含:

import 'package:audioplayers/audioplayers.dart';

(通常使用audiplayers时已经导入了这个包,UrlSource就包含在其中)

内容的提问来源于stack exchange,提问作者Ashish Magar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 05:48:24