Reputation: 643
I tried uploading image from flutter to firebase storage but when I try to do so nothing changes. I have made connection from firebase to flutter and when I run the code this error appears: [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: [core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()
. Also when I execute the code the showDialog method isn't poping off (This is a popup screen). Thanks in advance :)
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",
));
I tried fixing the code as @jitesh mentioned but still the same error, the code:
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"),
);
});
Upvotes: 2
Views: 758
Reputation: 34200
Use StorageTaskSnapshot
and wait for the upload completion, the rest code is self-explanatory.
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"),
);
}
Also, it seems you haven't initialized firebase, add below code in the base widget
@override
void initState() {
super.initState();
setUpFirebase();
}
void setUpFirebase() async {
await Firebase.initializeApp();
}
for the new ImagePicker
API get a File like this
final picker = ImagePicker();
final pickedFile = await picker.getImage(source: ImageSource. gallery);
File image = new File(pickedFile.path);
Upvotes: 1