0

I am new to Flutter and I am trying to make a wallpaper app where I use Firebase to store my images. The app retrieves images from Firebase and the user can share and download the images to the device. I'm using image_gallery_saver package but I wasn't able to get the url of the images so I can add it to the image_gallery_saver function, is there a simple way to get the url of an image from firebase after the user clicks on a specific image?

The following is the home page:

import 'package:cardstest2/Screens/ImageScreen.dart';
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'dart:async';




class FirestoreListViewPage extends StatefulWidget {
  @override
  _FirestoreListViewPageState createState() => _FirestoreListViewPageState();
}

class _FirestoreListViewPageState extends State<FirestoreListViewPage> {

  Future _data;

  Future getPosts() async {
    var firestore = Firestore.instance;
    QuerySnapshot qn = await firestore.collection("gallery").getDocuments();
    return qn.documents;
  }


  @override
  void initState() {
    super.initState();

    _data = getPosts();
  }


  @override
  Widget build(BuildContext context) {

    return new Scaffold(
      appBar: new AppBar(
        title: Text('Subcategories'),
      ),
      body: Container(
        child: FutureBuilder(
            future: _data,
            builder: (_, snapshot) {
              if (snapshot.connectionState == ConnectionState.waiting) {
                return Center(
                  child: Text('Waiting...'),
                );
              } else {

                return ListView.builder(
                    itemCount: snapshot.data.length,
                    itemBuilder: (_, index){

                      return Card(
                        child: ListTile(
                          title: Image.network(snapshot.data[index].data['GalleryUrl']),
                          onTap: () {
                            Navigator.of(context).push(
                              MaterialPageRoute(
                                builder: (context) => imageScreenPage(snapshot.data[index].data['GalleryUrl']),
                              ),
                            );
                          },
                        ),
                      );

                    });

              }
            }),
      ),
    );
  }
}

The following is the imageScreen page:

import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:share/share.dart';
import 'package:dio/dio.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'dart:ui';



class imageScreenPage extends StatefulWidget {
  String cardPath;

  imageScreenPage(this.cardPath);

  @override
  _imageScreenPageState createState() => _imageScreenPageState();
}

class _imageScreenPageState extends State<imageScreenPage> {

  final LinearGradient backgroundGradient = new LinearGradient(
      colors: [new Color(0x10000000), new Color(0x30000000)],
      begin: Alignment.topLeft,
      end: Alignment.bottomRight);


  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new SizedBox.expand(
        child: new Container(
          decoration: new BoxDecoration(gradient: backgroundGradient),
          child: new Stack(
            children: <Widget>[
              new Align(
                alignment: Alignment.center,
                child: new Hero(
                  tag: widget.cardPath,
                  child: new Image.network(widget.cardPath),

                ),

              ),
              new Align(
                alignment: Alignment.topCenter,
                child: new Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    new AppBar(
                      elevation: 0.0,
                      backgroundColor: Colors.transparent,
                      leading: new IconButton(
                          icon: new Icon(
                            Icons.close,
                            color: Colors.black,
                          ),
                          onPressed: () => Navigator.of(context).pop()),
                    )
                  ],
                ),
              ),


            ],
          ),

        ),
      ),


      persistentFooterButtons: <Widget>[
        IconButton(
          icon: Icon(Icons.wallpaper), onPressed: () {},
        ),
        IconButton(
          icon: Icon(Icons.file_download), onPressed: () {_save();},
        ),
        IconButton(
          icon: Icon(Icons.share), onPressed: () {Share.share(widget.cardPath);},
        ),
      ],

    );
  }

  _save() async {
    var response = await Dio().get("<insert url>", options: Options(responseType: ResponseType.bytes));
    final result = await ImageGallerySaver.saveImage(Uint8List.fromList(response.data));
    print(result);
  }



}

2 Answers 2

1

To get the downloadUrl, then do the following:

          StorageTaskSnapshot snapshot = await storage
              .ref()
              .child("images/$imageName")
              .putFile(file)
              .onComplete;
          if (snapshot.error == null) {
            final String downloadUrl =
                await snapshot.ref.getDownloadURL();
              }

use putFile to add the file to Firebase Storage, then you can use snapshot.ref.getDownloadURL() to get the url.

2
  • The app doesn't include user authentication and the user doesn't upload images to Firebase. The user just browses through the collection of images and can share an image or download it to the device. The images are retrieved from Firebase.
    – melonpun
    May 17, 2020 at 20:44
  • Use sign in anonymously of firebase to allow user to access the storage with creating an account. Aug 21, 2020 at 17:52
-1

For all the people using flutter in 2021

This worked for me

    import 'package:firebase_storage/firebase_storage.dart' as firebase_storage;

    Future<String> uploadImage(imageFile) async {
    firebase_storage.Reference ref = storage.ref().child('post_$postId.jpg');
    firebase_storage.UploadTask uploadTask = ref.putFile(imageFile);
    print('File Uploaded');
    var imageUrl = await (await uploadTask).ref.getDownloadURL();
    String url = imageUrl.toString();
    return url;
  }

uploadImage() is a function which takes in an imageFile

You have to call it using await uploadImage(imageFile) inside an asynchronous function

1
  • This work for me isn't enough....please add some explanation to your answer
    – Sfili_81
    Jun 15, 2021 at 13:19

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.