繁体   English   中英

Flutter 从 firebase 存储中获取图像并在应用程序中显示以供下载

[英]Flutter get image from firebase storage and show it in app for download

我正在尝试从 firebase 存储(firebase firestore 中的 url)获取图像,并像过滤器一样在我的应用程序中显示它。

我的代码(它不起作用):

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:pap_test/upload/upload_resumos_screen.dart';

class DownloadScreen extends StatefulWidget {
  const DownloadScreen({Key? key}) : super(key: key);
  static const routeName = '/downlaod-resumos';

  @override
  State<DownloadScreen> createState() => _DownloadScreenState();
}

class _DownloadScreenState extends State<DownloadScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Resumos'),
      ),
      body: StreamBuilder(
        stream: FirebaseFirestore.instance.collection('courses').snapshots(),
        builder: (context, AsyncSnapshot snapshot) {
          return ListView.builder(
            itemCount: snapshot.data.documents.length,
            itemBuilder: (context, index) {
              DocumentSnapshot image = snapshot.data!.documents[index];
              return ListTile(
                leading: Image.network(
                  image['disciplina'],
                ),
                title: Text(image['modulo']),
                subtitle: Text('11'),
              );
            },
          );
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          // Add your onPressed code here!
          Navigator.of(context).pushNamed(UploadResumoScreen.routeName);
        },
        backgroundColor: Color.fromARGB(255, 28, 209, 216),
        child: const Icon(Icons.add),
      ),
    );
  }
}

我为每个图像创建了一个文档,在文档中我保存了 disciplina 和模数(我试图过滤的东西),以及 url 转到存储在存储中的图像。 数据库火力库存储

我想像列表视图一样展示它们

我如何从 firebase 存储和 firebase firestore 中的 url 中获取图像并使用过滤器在我的应用程序中显示?

应该是这样的:

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

firbaseStorage.Reference storageRef = storage.ref('NameOfFolder');
......
final imgUrl = await storageRef
        .child('ImagePath')
        .getDownloadURL();

对于存储,请查看 文档 如果您必须遍历异步列表 function,也请尝试查看这些答案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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