i want to upload image to firebase stroge but i face this error(The body might complete normally, causing 'null' to be returned, but the return type, 'FutureOr', is a potentially non-nullable type. Try adding either a return or a throw statement at the end.)
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);
}
}
}
CodePudding user response:
If you are not sure about a return statement then either you can use void
or dynamic
as the return type.
If you will use void
then you are not allowed to use return, but in case if you use dynamic
then you can either use a return statement or not.
And if it is the case with an async function then you can enclose the return type under Future<return_type>
. For your case you might want Future<dynamic>
or just dynamic
will work.
CodePudding user response:
For this function I think you don't need to return any thing. just make return type void .
void uploadFile
And any void function can use return keyword in body like this :
return ;