我需要帮助,我正在使用 flutter 和 firebase 创建我的应用程序, 在那里,我想将图像上传到 firebase 存储。 所以我的文件
import 'package:firebase_storage/firebase_storage.dart';
uploadTOFirebase() async {
final ref = FirebaseStorage.instance.ref('posts/post_$postId.jpg');
await ref.putFile(image).whenComplete(() async {
final url = await ref.getDownloadURL();
setState(() {
postlink = url;
});
// print(postlink);
});
}
但是我看到了那个错误
E/flutter (19469): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: NoSuchMethodError: The method 'ref' was called on null.
E/flutter (19469): Receiver: null
E/flutter (19469): Tried calling: ref("posts/post_27cfad88-62bb-4211-9e5b-0c6d1e9029be.jpg")
E/flutter (19469): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
E/flutter (19469): #1 _UploadState.uploadTOFirebase (package:instaclone/pages/upload.dart:230:48)
E/flutter (19469): #2 _UploadState.HandleSubmit (package:instaclone/pages/upload.dart:226:11)
E/flutter (19469): <asynchronous suspension>
E/flutter (19469):
我在andoird中使用flutter
答案 0 :(得分:0)
尝试像这样使用它。
final ref = FirebaseStorage.instance.ref().child("posts/post_$postId.jpg");
答案 1 :(得分:0)
你有没有在任何地方给 Firebase.initializeApp()
打电话?否则,Firebase SDK 将无法在 Google 的服务器上找到您的项目。
有关如何执行此操作的示例,请参阅 FlutterFire 文档中的 initializing FlutterFire。
答案 2 :(得分:0)
uploadTOFirebase() async {
UploadTask storageUploadTask = FirebaseStorage.instance.putFile('posts/post_$postId.jpg');
TaskSnapshot storageTaskSnapshot =
await storageUploadTask.whenComplete(() => null);
String url = await storageTaskSnapshot.ref.getDownloadURL();
setState((){postlink = url;});
}
试试这个代码。做了一些小改动。