【发布时间】:2021-10-25 12:33:00
【问题描述】:
我是 Flutter 的新手,我正在尝试编写一个广播流媒体应用程序,我使用 just_audio 来处理我的播放器。当我在调试模式下启动我的应用程序时,它为我提供了用于空值消息的空检查运算符。我尝试添加一个 if 条件来检查是否为 null,但效果不佳。
这是我的小部件代码:
@override
Widget build(BuildContext context) {
return new WillPopScope(
onWillPop: () async => false,
child: new MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Container(
decoration: new BoxDecoration(
image: new DecorationImage(image: AssetImage("assets/img/MobilePlayer.png"), fit: BoxFit.cover),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
child: StreamBuilder<SequenceState?>(
stream: _player.sequenceStateStream,
builder: (context, snapshot) {
final state = snapshot.data;
if (state?.sequence.isEmpty ?? true) return SizedBox();
final metadata = state!.currentSource!.tag as AudioMetadata;
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child:
Center(
child: Image.network(metadata.artwork,
scale: 1.5,
),
),
),
),
// Text(metadata.album,
// style: Theme.of(context).textTheme.headline6),
// Text(metadata.title),
],
);
},
),
),
ControlButtons(_player),
// StreamBuilder<PositionData>(
// stream: _positionDataStream,
// builder: (context, snapshot) {
// final positionData = snapshot.data;
// return SeekBar(
// duration: positionData?.duration ?? Duration.zero,
// position: positionData?.position ?? Duration.zero,
// bufferedPosition:
// positionData?.bufferedPosition ?? Duration.zero,
// onChangeEnd: (newPosition) {
// _player.seek(newPosition);
// },
// );
// },
// ),
SizedBox(height: 8.0),
Row(
children: [
],
),
Container(
height: 300.0,
color: Colors.transparent,
**child: StreamBuilder<SequenceState?>(**
stream: _player.sequenceStateStream,
builder: (context, snapshot) {
final state = snapshot.data;
final sequence = state?.sequence ?? [];
final metadata = state!.currentSource!.tag as AudioMetadata;
return ListView(
children: [
for (var i = 0; i < sequence.length; i++)
Dismissible(
key: ValueKey(sequence[i]),
background: Container(
color: Colors.transparent,
alignment: Alignment.centerRight,
child: Padding(
padding: const EdgeInsets.only(right: 8.0),
child: Icon(Icons.delete, color: Colors.transparent),
),
),
onDismissed: (dismissDirection) {
_playlist.removeAt(i);
},
child: Material(
color: i == state.currentIndex
? Colors.transparent
: Colors.transparent,
child: Card(
color: Colors.transparent,
shape: StadiumBorder(
side: BorderSide(
color: i == state.currentIndex
? Colors.yellow
: Colors.white,
width: 1.0,
),
),
child: ListTile(
title: Text(sequence[i].tag.title as String,
style: GoogleFonts.lato(
textStyle: TextStyle(color: i == state.currentIndex
? Colors.yellow
: Colors.white,
fontSize: 20.0,
fontWeight: FontWeight.w700,
),
),
textAlign: TextAlign.center,
),
onTap: () {
_player.seek(Duration.zero, index: i);
},
leading: Wrap(
children: <Widget>[
IconButton(icon: i == state.currentIndex
? Icon(Icons.pause, color: i == state.currentIndex
? Colors.yellow
: Colors.white,)
: Icon(Icons.play_arrow,
color: i == state.currentIndex
? Colors.yellow
: Colors.white,),
onPressed: (){
_player.seek(Duration.zero, index: i);
i == state.currentIndex
? _player.pause()
: _player.play();
}
),
],
),
trailing: Wrap(
children: <Widget>[
IconButton(icon: Icon(FontAwesomeIcons.whatsapp,
color: i == state.currentIndex
? Colors.yellow
: Colors.white,),
onPressed: () async => await launch(sequence[i].tag.wpp as String)
)
],
),
),
),
),
),
],
);
},
),
),
],
),
),
),
),
);
}
【问题讨论】:
-
在哪一行?你使用
!在什么是空的。 -
嘿,谢谢你的回答,我的终端在这一行指责这个错误:child: StreamBuilder
(