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;
@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),
),
CodePudding user response:
Don't return the ListView.builder
if the records
is null.
return widget.records == null ? SizedBox() : ListView.builder...
CodePudding user response:
Declare your class like this,
class Record extends StatefulWidget {
const Record({
Key? key,
required this.records,
}) : super(key: key);
final List records;
@override
_RecordState createState() => _RecordState();
}
In the above code records
is a list of type List<dynamic>
. If you have a specific data type,
final List<YOUR_DATA_TYPE> records;
This way, records will never be null. If records
can be null,
final List<YOUR_DATA_TYPE>? records;
and use,
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),
),