【问题标题】:flutter:: NoSuchMethodError: The getter 'length' was called on nullflutter:: NoSuchMethodError: getter 'length' 在 null 上被调用
【发布时间】:2021-11-29 20:08:05
【问题描述】:

我创建了一个功能,可以在录制时将录制的声音显示在屏幕上。 但是,如果小部件中没有记录(空),则出现上述错误,因为无法使用长度。我该如何解决这个问题?

class Record extends StatefulWidget {
  const Record({
    Key? key,
  }) : super(key: key);

  get records => null;


  @override
  _RecordState createState() => _RecordState();
}

class _RecordState extends State<Record> {
  late int _totalTime;
  late int _currentTime;
  double _percent = 0.0;
  int _selected = -1;
  bool isPlay=false;
  AudioPlayer advancedPlayer = AudioPlayer();

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: widget.records.length,
      shrinkWrap: true,
      reverse: true,
      itemBuilder: (BuildContext context, int i) {
        return Card(
          elevation: 5,
          child: ExpansionTile(
            title: Text(
              'Record ${widget.records.length - i}',
              style: TextStyle(color: Colors.black),
            ),

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    如果records 为空,则不要返回ListView.builder

    return widget.records == null ? SizedBox() : ListView.builder...
    

    【讨论】:

      【解决方案2】:

      这样声明你的类,

      class Record extends StatefulWidget {
        const Record({
          Key? key,
          required this.records,
        }) : super(key: key);
      
        final List records;
      
        @override
        _RecordState createState() => _RecordState();
      }
      

      在上面的代码中records 是一个List&lt;dynamic&gt; 类型的列表。如果您有特定的数据类型,

      final List<YOUR_DATA_TYPE> records;
      

      这样,记录永远不会为空。如果records可以为空,

      final List<YOUR_DATA_TYPE>? records;
      

      并使用,

      return ListView.builder(
            itemCount: widget.records?.length ?? 0,
            shrinkWrap: true,
            reverse: true,
            itemBuilder: (BuildContext context, int i) {
              return Card(
                elevation: 5,
                child: ExpansionTile(
                  title: Text(
                    'Record ${widget.records.length - i}',
                    style: TextStyle(color: Colors.black),
                  ),
      

      【讨论】:

        【解决方案3】:

        jiji.!

        它返回 null 因为在某些情况下,Listview 在将值分配给构建中的widget.records 之前执行。在这种情况下,即使widget.records 中有记录,您也可能会得到空列表。

        所以,你只需要处理这个空异常。喜欢,

        ListView.builder(
        shrinkWrap: true,
        reverse: true,
        physics: BouncingScrollPhysics(),
        itemCount: widget.records.length,
        itemBuilder: (BuildContext context, int index) =>
            widget.customer == null
                ? Card(
                    elevation: 5,
                    child: ListTile(
                      title: Text(
                        'No Records Found',
                        style: TextStyle(
                          color: Colors.black,
                          fontSize: 14,
                        ),
                      ),
                    ),
                  )
                : widget.records.isEmpty
                    ? Card(
                        elevation: 5,
                        child: ListTile(
                          title: Text(
                            'No Records Available',
                            style: TextStyle(
                              color: Colors.black,
                              fontSize: 14,
                            ),
                          ),
                        ),
                      )
                    : Card(
                        elevation: 5,
                        child: ExpansionTile(
                          title: Text(
                            'Record ${widget.records.length - index}',
                            style:
                                TextStyle(color: Colors.black),
                          ),
                        ),
                      ),
        ),
        

        它将检查 NULL、空列表With Records 列表。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-12-03
          • 2020-06-15
          • 1970-01-01
          • 2021-08-11
          • 2021-12-10
          • 2021-05-25
          • 2023-03-08
          相关资源
          最近更新 更多