首页 > 解决方案 > 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)
                                    )
                                  ],
                                ),
                              ),
                            ),
                          ),
                        ),
                      ],
                    );
                  },
                ),
              ),
            ],
          ),
        ),
      ),
    ),
    );
    
  }

标签: flutterdart

解决方案


这是 Flutter 中的一个常见问题,这个错误意味着你标记了一个永远不会null带有这个!标记的变量。但是当你的程序运行时,这个值变成了null.

在您的代码中,您已!在多行中使用了此标记。简单地说,在那里放置一个调试指针并确定它何时变为空。

此外,您应该尽可能使用以下方法。

String hello = state?.value ?? 'default';

而不是这个:

String hello = state!.value

推荐阅读