【问题标题】:Flutter/Dart - The getter 'path' was called on null. Receiver: null Tried calling: pathFlutter/Dart - 在 null 上调用了 getter 'path'。接收方:null 尝试调用:路径
【发布时间】:2020-07-30 20:25:30
【问题描述】:

我正在使用基于flutter_audio_recorder 的 playerWidget。虽然它可以按要求工作,但在编译和热重启时我会收到一条恼人的错误消息错误,因为路径一开始是空的。该路径为空,因为尚未加载任何音频文件。但是一旦我使用的PageView.builder 完成,音频就会被加载,因此错误消息对实际操作没有影响。但是,错误消息变得烦人。所以我想从我的服务器提供一个默认的音频文件,这样错误就会停止。

错误信息说

  ════════ 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

而调用错误的Widget是;

child: PlayerWidget(url: _current.path),

我尝试了以下方法;

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

但它不能解决 path is null 错误。谁能看到我做错了什么?

【问题讨论】:

  • 这个错误意味着_currentnull 不管它是什么。您可以改为写_current?.path ?? 'http://example.com/ping.m4a';
  • 成功了!我怎样才能给你信用?你能正式回答这个问题吗?

标签: flutter dart null conditional-statements


【解决方案1】:

在 null 上调用了 getter 'path'。

此错误意味着您正在为其编写object.path 的对象是null。您可以像这样使用?. 运算符:object?.something,相当于:

object!=null ? object.something : null

您可以将它与您已经在使用的?? 运算符配对。因此,您只需将您的声明更改为:

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

【讨论】:

    猜你喜欢
    • 2020-11-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2021-09-07
    • 2023-03-20
    • 1970-01-01
    • 2021-08-24
    • 2021-09-13
    相关资源
    最近更新 更多