首页 > 解决方案 > 颤振:处理 File.path 空/空

问题描述

我有File名字 _pickedImage。我面临处理 File.path null 的问题,我已经path用这段代码处理了 null :

 if (pickedImage.path == null)

或者

if (pickedImage.path.isEmpty)

每次我按下按钮时,我都无法处理 File.pathnull并且我收到此错误。

═══════ Exception caught by gesture ═══════════════════════════════════════════
The following NoSuchMethodError was thrown while handling a gesture:
The getter 'path' was called on null.
Receiver: null
Tried calling: path

Handler: "onTap"
Recognizer: TapGestureRecognizer#c9d89
    debugOwner: GestureDetector
    state: possible
    won arena
    finalPosition: Offset(246.8, 613.3)
    finalLocalPosition: Offset(246.8, 23.3)
    button: 1
    sent tap down

我做错了吗?

标签: flutterdart

解决方案


═══════ Exception caught by gesture ═══════════════════════════════════════════
The following NoSuchMethodError was thrown while handling a gesture:
The getter 'path' was called on null.
Receiver: null
Tried calling: path

该错误仅仅是因为您试图从null File.

在调用下一个代码之前,您需要检查文件是否为空:

 if (pickedImage == null) {
   // File is null, probably not found or incorrect path.
   return;
 }

 // Do something with the file
 if (pickedImage.path == null) {

 }

推荐阅读