将图像从 flutter 上传到 firebase(没有任何反应)
Uploading image from flutter to firebase (nothing happens)
我尝试将图像从 flutter 上传到 firebase 存储但是当我尝试这样做时没有任何变化。我已经建立了从 firebase 到 flutter 的连接,当我 运行 代码出现此错误时:[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: [core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()
。此外,当我执行代码时,showDialog 方法不会弹出(这是一个弹出屏幕)。提前致谢:)
class _MyHomePageState extends State<MyHomePage> {
Future <File> file;
//final storage = FirebaseStorage.instance;
openGatePhoto() async{
final file = await ImagePicker.pickImage(source: ImageSource.gallery);
var reference = FirebaseStorage.instance.ref().child('last_image/car5');
await reference.putFile(file);
//var snapshot = await storage.ref().child('last_image/car5.png').putFile(file).onComplete;
showDialog(
context: context,
builder: (context) => CustomDialogOpen(
title: "Gates are open",
description:
"Licence plate numbers have been successfully stored in the database",
));
我尝试修复@jitesh提到的代码,但仍然是同样的错误,代码:
final file = await ImagePicker.pickImage(source: ImageSource.gallery);
Reference reference =
FirebaseStorage.instance.ref().child('last_image/car5');
FirebaseStorage storage = FirebaseStorage.instance;
UploadTask uploadTask = reference.putFile(file);
uploadTask.then((res) {
res.ref.getDownloadURL().then(
(value) => print("Done: $value"),
);
});
使用StorageTaskSnapshot
等待上传完成,其余代码不言自明。
Future uploadImageToFirebase(BuildContext context) async {
final file = await ImagePicker.pickImage(source: ImageSource.gallery);
var reference = FirebaseStorage.instance.ref().child('last_image/car5');
StorageUploadTask uploadTask = reference.putFile(file);
StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
taskSnapshot.ref.getDownloadURL().then(
(value) => print("Done: $value"),
);
}
此外,您似乎还没有初始化 firebase,请在基础小部件中添加以下代码
@override
void initState() {
super.initState();
setUpFirebase();
}
void setUpFirebase() async {
await Firebase.initializeApp();
}
对于新的 ImagePicker
API 得到这样的文件
final picker = ImagePicker();
final pickedFile = await picker.getImage(source: ImageSource. gallery);
File image = new File(pickedFile.path);
我尝试将图像从 flutter 上传到 firebase 存储但是当我尝试这样做时没有任何变化。我已经建立了从 firebase 到 flutter 的连接,当我 运行 代码出现此错误时:[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: [core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()
。此外,当我执行代码时,showDialog 方法不会弹出(这是一个弹出屏幕)。提前致谢:)
class _MyHomePageState extends State<MyHomePage> {
Future <File> file;
//final storage = FirebaseStorage.instance;
openGatePhoto() async{
final file = await ImagePicker.pickImage(source: ImageSource.gallery);
var reference = FirebaseStorage.instance.ref().child('last_image/car5');
await reference.putFile(file);
//var snapshot = await storage.ref().child('last_image/car5.png').putFile(file).onComplete;
showDialog(
context: context,
builder: (context) => CustomDialogOpen(
title: "Gates are open",
description:
"Licence plate numbers have been successfully stored in the database",
));
我尝试修复@jitesh提到的代码,但仍然是同样的错误,代码:
final file = await ImagePicker.pickImage(source: ImageSource.gallery);
Reference reference =
FirebaseStorage.instance.ref().child('last_image/car5');
FirebaseStorage storage = FirebaseStorage.instance;
UploadTask uploadTask = reference.putFile(file);
uploadTask.then((res) {
res.ref.getDownloadURL().then(
(value) => print("Done: $value"),
);
});
使用StorageTaskSnapshot
等待上传完成,其余代码不言自明。
Future uploadImageToFirebase(BuildContext context) async {
final file = await ImagePicker.pickImage(source: ImageSource.gallery);
var reference = FirebaseStorage.instance.ref().child('last_image/car5');
StorageUploadTask uploadTask = reference.putFile(file);
StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
taskSnapshot.ref.getDownloadURL().then(
(value) => print("Done: $value"),
);
}
此外,您似乎还没有初始化 firebase,请在基础小部件中添加以下代码
@override
void initState() {
super.initState();
setUpFirebase();
}
void setUpFirebase() async {
await Firebase.initializeApp();
}
对于新的 ImagePicker
API 得到这样的文件
final picker = ImagePicker();
final pickedFile = await picker.getImage(source: ImageSource. gallery);
File image = new File(pickedFile.path);