Flutter/Dart - The getter 'path' was called on null. Receiver: null Tried calling: path

Viewed 5123

I'm using a playerWidget based on flutter_audio_recorder. Though it works as required I get an annoying error message error upon compiles and hot restarts because the path is null at first. The path is null because no audio files have yet loaded. But once the PageView.builder which I'm using completes, the audio is then loaded so the error message has no effect on the actual operation. However the error message is becoming annoying. So I'd like to provide a default audio file from my server so the error will stop.

The error message says

  ════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following NoSuchMethodError was thrown building SpeakRecorder(dirty, dependencies: [_InheritedProviderScope<SocialProvider>], state: SpeakRecorderState#5e63d):
The getter 'path' was called on null.
Receiver: null
Tried calling: path

And the Widget which calls the error is;

child: PlayerWidget(url: _current.path),

I tried the following;

String defaultaudio = _current.path ?? 'http://example.com/ping.m4a';

But it doesn't resolve the path is null error. Can anybody see what I'm doing wrong?

1 Answers

The getter 'path' was called on null.

This error means the the object for which you are writing object.path is null. You can use ?. operator like this: object?.something which is equivalent to:

object!=null ? object.something : null

You can pair it with ?? operator which you are already doing. So you only have to change your statement to:

String defaultaudio = _current?.path ?? 'http://example.com/ping.m4a';
Related