Solved: flutter:: NoSuchMethodError: The getter 'length' was called on null


Asked by jiji on November 29, 2021 (source).

I created a function where the recorded voice is displayed on the screen when recording. However, if there is no record(null) in the widget, the above error occurs because length cannot be used. How can I solve this?

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

  get records => null;

  _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();

  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:,


Question answered by esentis (source).

Don't return the ListView.builder if the records is null.

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