I am calling a simple function that returns a duration object. However, it is null at times. I want to modify the code so that it can return only a duration object and not duration? object.
factory ExecutionModel.fromSnapshot(
DocumentSnapshot<Map<String, dynamic>> snapshot) {
final data = snapshot.data()!;
return ExecutionModel(
selfRef: snapshot.reference,
state: TimerState.fromString(data['state']),
timeAdded: fromJsonDuration(data['duration'] as int), // this line
activeTimerRef: data['activeTimerRef'],
paused: fromJsonDateTime(data['paused'] as Timestamp),
started: fromJsonDateTime(data['started'] as Timestamp));
Duration fromJsonDuration(int milliseconds) {
return Duration(milliseconds: milliseconds);
}
Here timeAdded argument is a Duration object and not Duration? object. But it can occasionally have null values. What is the fix around for this? I don't want to change timeAdded to Duration? as it can affect other areas.
3
Answers
Try this method –
Better solution:
Try this