Flutter:Firebase存储下载网址

问题描述 投票:0回答:1

我是Flutter的新手,我正在尝试制作一个壁纸应用程序,在其中使用Firebase存储图像。该应用程序从Firebase检索图像,用户可以共享图像并将其下载到设备。我正在使用image_gallery_saver程序包,但无法获取图像的URL,因此可以将其添加到image_gallery_saver函数中,有没有一种简单的方法可以在用户单击特定对象后从Firebase获取图像的URL。图片?

以下是主页:

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']),
                              ),
                            );
                          },
                        ),
                      );

                    });

              }
            }),
      ),
    );
  }
}

以下是imageScreen页面:

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);
  }



}
firebase flutter url download
1个回答
0
投票

要获取downloadUrl,然后执行以下操作:

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

使用putFile将文件添加到Firebase存储,然后您可以使用snapshot.ref.getDownloadURL()来获取URL。

© www.soinside.com 2019 - 2024. All rights reserved.