flutter - Flutter 显示在应用程序启动时对空值使用的空值检查运算符
问题描述
我是 Flutter 的新手,我正在尝试编写一个广播流媒体应用程序,我使用 just_audio 来处理我的播放器。当我在调试模式下启动我的应用程序时,它为我提供了用于空值消息的空检查运算符。我已经尝试添加一个 if 条件来检查是否为空,但它并不完全有效。
这是我的小部件代码:
@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)
)
],
),
),
),
),
),
],
);
},
),
),
],
),
),
),
),
);
}
解决方案
这是 Flutter 中的一个常见问题,这个错误意味着你标记了一个永远不会null
带有这个!
标记的变量。但是当你的程序运行时,这个值变成了null
.
在您的代码中,您已!
在多行中使用了此标记。简单地说,在那里放置一个调试指针并确定它何时变为空。
此外,您应该尽可能使用以下方法。
String hello = state?.value ?? 'default';
而不是这个:
String hello = state!.value
推荐阅读
- 6502 - 6502 程序集从内存块中获取数据
- asp.net - 知道当前 url 是否包含在 RouteTable.Routes 中
- python - 如何检查 API 响应的编码方案
- php - 检查单选按钮是否被点击(选中) - Smarty 模板
- c# - 根据 DateTimePicker 值在 Datagridview 中显示结果
- php - 阻止特定文件夹中的特定 PHP 文件
- python - Python,简单的压缩/编码算法
- javascript - TypeError:“listener”参数必须是一个函数。在节点 JS 中使用 npm pixelmatch
- javascript - 一个简单的反应应用程序中的数据结构
- jquery - 无法捕获 id($('.input_fields_wrap').append(h1 tag)) 的值。并将值存储在 var 数据中