简体   繁体   中英

How to Load Image to Firebase Storage and get downloadUrl (Flutter/Firebase)

I want to implement sending files to Firebase Storage, as well as get a link to it. But for some reason it does not work ...

void _pickFile() async {
  File file = await FilePicker.getFile(type: FileType.ANY);
  StorageReference ref = FirebaseStorage.instance.ref()
  .child("image${Random().nextInt(999)}.jpg");
  var fileUrl = ref.putFile(file).onComplete.then((file) => 
  file.ref.getDownloadURL());
  print(fileUrl);
  _sendMessage(fileUrl: fileUrl.toString());
}

...
prefixIcon: IconButton(
  icon: Icon(Icons.attach_file),
  color: Colors.white,
  onPressed: () => _pickFile()
)

Why do I get this instead of a link?

I/flutter (16610): Instance of 'Future'

I need a link in the String!

What is the problem?

Like the log say, fileUrl is a Future who is not resolved yet. You should make sure it's resolve to make use of the string give.

void _pickFile() async {
  File file = await FilePicker.getFile(type: FileType.ANY);
  StorageReference ref = FirebaseStorage.instance.ref()
    .child("image${Random().nextInt(999)}.jpg");
  String fileUrl = await (await ref.putFile(file).onComplete).ref.getDownloadURL();
  print(fileUrl);
  _sendMessage(fileUrl: fileUrl.toString());
}

You need use await for Future functions

void _pickFile() async {
  File file = await FilePicker.getFile(type: FileType.ANY);
  StorageReference ref = FirebaseStorage.instance.ref()
  .child("image${Random().nextInt(999)}.jpg");
  final StorageUploadTask uploadTask = ref.putFile(file);
  StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
  String downloadUrl = await taskSnapshot.ref.getDownloadURL();
  _sendMessage(fileUrl: downloadUrl);
}

It would look like this without using await

void _pickFile() async {
  File file = await FilePicker.getFile(type: FileType.ANY);
  StorageReference ref = FirebaseStorage.instance.ref()
  .child("image${Random().nextInt(999)}.jpg");
  ref.putFile(file).onComplete.then((file){
    var fileUrl = file.ref.getDownloadURL());
    _sendMessage(fileUrl: fileUrl.toString());
  });
}

here is my solution to upload and get the uploaded image url

first i get image with image picker

Future getImage() async {
  var image = await ImagePicker.pickImage(source: ImageSource.gallery);

  setState(() {
    _image = image;
      print('Image Path $_image');
  });
}

then i upload to firebase

Future uploadPic(BuildContext context) async {

  String fileName = basename(_image.path);
  StorageReference firebaseStorageRef = FirebaseStorage.instance.ref().child(fileName);
  StorageUploadTask uploadTask = firebaseStorageRef.putFile(_image);
  StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;


  final String url = (await taskSnapshot.ref.getDownloadURL());
  print('URL Is $url');

  setState(() {
    print("Profile Picture uploaded");
    print("....................$url");
    Scaffold.of(context).showSnackBar(
        SnackBar(content: Text('Profile Picture Uploaded')));
  });
}

this is the part i get the downloadUrl of currently uploaded image

  final String url = (await taskSnapshot.ref.getDownloadURL());
  print('URL Is $url');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM