Uploading image from flutter to firebase (nothing happens)
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);
Comments
-
Don over 1 year
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"), ); });
-
Don almost 3 yearsFor StorageUploadTask and StorageTaskSnapshot it displays that it want me to make a class/method
-
Don almost 3 yearsSaying error: 'StorageUploadTask' isn't a type. StorageUploadTask uploadTask = reference.putFile(file);
-
Jitesh Mohite almost 3 yearsyou need to initialize firebase, have a look at the edited answer.
-
Don almost 3 yearsafter initializing it, it worked. Thank you :)
-
s3v3ns almost 3 yearsHope it's not too late to ask a question here. I am getting error: The argument type 'PickedFile?' can't be assigned to the parameter type 'File' everything is the same. final pickedFile =... And the error comes here UploadTask uploadTask = reference.putFile(pickedFile); on pickedFile
-
Jitesh Mohite almost 3 years@s3v3ns: could you ask a new question for it and ping link here