Flutter: Copy image file from url to firebase

1,401

File only supports files on a file system. To load content using HTTP use the http package. See also https://flutter.io/networking/

var httpClient = createHttpClient();
var response = await httpClient.get(url); 

and then get the data from response.body, or

var response = await httpClient.readBytes(url);

to get it as binary (Uint8List)

See also https://www.dartdocs.org/documentation/http/0.11.3+14/http/Client-class.html

Share:
1,401
FrederickCook
Author by

FrederickCook

Updated on December 03, 2022

Comments

  • FrederickCook
    FrederickCook over 1 year

    I'm trying to copy a user profile picture from an external service onto my firebase server. So far I have:

    final File file = await new File.fromUri(Uri.parse(auth.currentUser.photoUrl)).create();
    final StorageReference ref = FirebaseStorage.instance.ref().child("profile_image_${auth.currentUser.uid}.jpg");
    final StorageUploadTask uploadTask = ref.put(file);
    final Uri downloadUrl = (await uploadTask.future).downloadUrl;
    
    // add user profile picture url to user object
    final userReference = FirebaseDatabase.instance
        .reference()
        .child('users/' + auth.currentUser.uid);
    userReference.set({'photoUrl': downloadUrl});
    

    The very top line gives me the error: Unsupported operation: Cannot extract a file path from a https URI

    What is the correct way to do this? Should this even be done client-side? (Should I just be passing this url to firebase and use a function to download it server-side?)