正文可能会正常完成,从而导致返回"null",但返回类型"FutureOr<Void>"可能是不可为空的类型。尝试添加



我想将图像上传到firebase stroge,但我面临此错误(body可能正常完成,导致'null'被返回,但返回类型'FutureOr'是一个潜在的非空类型)。尝试在末尾添加return或throw语句。)

import 'dart:ffi';
import 'dart:io';

import 'package:firebase_storage/firebase_storage.dart' as firebase_storage;
import 'package:firebase_core/firebase_core.dart' as firebase_core;

class Storage {
final firebase_storage.FirebaseStorage storage =
firebase_storage.FirebaseStorage.instance;

Future<Void> uploadFile(//The body might complete normally, causing 'null' to be returned, but the return type, 'FutureOr<Void>', is a potentially non-nullable type.
Try adding either a return or a throw statement at the end.
String filepath,
String filename,
) async {
File file = File(filepath);

try {
await storage.ref('pizza/$filename').putFile(file);
} on firebase_core.FirebaseException catch (e) {
print(e);
}

}
}

如果您不确定返回语句,那么您可以使用voiddynamic作为返回类型。

如果使用void,则不允许使用return,但如果使用dynamic,则可以使用return语句或不使用return语句。

如果是async函数的情况,那么您可以将返回类型包含在Future<return_type>下。对于您的情况,您可能需要Future<dynamic>dynamic将工作。

相关内容

最新更新