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

4,744

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';
Share:
4,744
Meggy
Author by

Meggy

I'm a self-taught novice stumbling like a drunk through php, javascript, mysql, drupal and flutter.

Updated on December 23, 2022

Comments

  • Meggy
    Meggy over 1 year

    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?

    • Ryosuke
      Ryosuke almost 4 years
      The error means that _current is null whatever it is. You can instead write _current?.path ?? 'http://example.com/ping.m4a';.
    • Meggy
      Meggy almost 4 years
      That did the trick! How can I can give you the credit? Can you answer the question formally?