Reputation: 11
I got a problem when I try to upload an image through Firebase Storage. This is the function to upload the file:
Future uploadFile() async {
int i = 1;
final String fileName = DateTime.now().toString();
for (var img in _image) {
setState(() {
val = i / _image.length;
});
FirebaseStorage storage = FirebaseStorage.instance;
Reference ref = FirebaseStorage.instance.ref('gs://face14-5d6e2.appspsot.com').child(fileName);
File file = File(img.path);
try {
await ref.putFile(file);
} on FirebaseException catch (e) {
print(e);
}
}
}
And this is the error log I'm getting:
/flutter (13708): [firebase_storage/unknown] location should not be a full URL. I/flutter (13708): /data/user/0/com.example.face1/cache/image_picker3492941265752856582.jpg E/flutter (13708): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: PlatformException(firebase_storage, location should not be a full URL., {}, null) E/flutter (13708): #0
StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:597:7) E/flutter (13708): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:158:18) E/flutter (13708): E/flutter (13708): I/flutter (13708): [firebase_storage/unknown] location should not be a full URL. I/flutter (13708): /data/user/0/com.example.face1/cache/image_picker1191392770962444186.jpg E/flutter (13708): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: PlatformException(firebase_storage, location should not be a full URL., {}, null) E/flutter (13708): #0
StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:597:7) E/flutter (13708): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:158:18) E/flutter (13708): E/flutter (13708): I/flutter (13708): [firebase_storage/unknown] location should not be a full URL.
Upvotes: 0
Views: 902
Reputation: 62
I've checked your code and notify that you didn't follow some instruction to implement flutter and firebase storage.
Please refer this link first How to implement cloud storage in flutter
Second, Uploading files in fire storage will take lots of seconds, so you need to give some long latency.
I will give you my code in flutter
class ImageUpload extends StatefulWidget {
@override
_ImageUploadState createState() => _ImageUploadState();
}
class _ImageUploadState extends State<ImageUpload> {
String imageUrl;
@override
Widget build(BuildContext context) {
backgroundColor: Colors.white,
return Scaffold(
appBar: AppBar(
title: Text('Upload Image', style: TextStyle(color: Colors.black87, fontWeight: FontWeight.bold),),
centerTitle: true,
elevation: 0.0,
backgroundColor: Colors.white,
),
body: Container()
);
}
}
Function to upload image to Firebase Storage
Here, we are going to implement a function called uploadImage
that will contain all the configuration from selecting an image from the gallery to uploading it to Firebase Storage to viewing the image in the Image Placeholder section after upload.
Upload code snippet:
uploadImage() async {
final _firebaseStorage = FirebaseStorage.instance;
final _imagePicker = ImagePicker();
PickedFile image;
//Check Permissions
await Permission.photos.request();
var permissionStatus = await Permission.photos.status;
if (permissionStatus.isGranted){
//Select Image
image = await _imagePicker.getImage(source: ImageSource.gallery);
var file = File(image.path);
if (image != null){
//Upload to Firebase
var snapshot = await _firebaseStorage.ref()
.child('images/imageName')
.putFile(file).onComplete;
var downloadUrl = await snapshot.ref.getDownloadURL();
setState(() {
imageUrl = downloadUrl;
});
} else {
print('No Image Path Received');
}
} else {
print('Permission not granted. Try Again with permission access');
}
}
Until next time folks, Happy coding!
Upvotes: 0
Reputation: 50930
Taking a look at the documentation (and as the error says), you don't need to pass the gs://domain.tld
part in the ref.
Reference ref = FirebaseStorage.instance.ref('/path/to/filename.txt');
This should work fine. Alternatively you can try this:
Reference ref = FirebaseStorage.instance.ref()
.child('path')
.child('to')
.child('filename.txt');
Upvotes: 0