我想在flutter中使用firebasestorage获取文件列表,但是我可以

发布于 2025-02-13 01:58:21 字数 2663 浏览 0 评论 0 原文

我是日本人。我正在使用自动翻译,所以如果我的英语在某些地方有些奇怪,请原谅我。

现在,我的问题是,我想将Firebasestorage和Flutter链接到此视频(下面的链接)以获取文件列表,但是我很难得到它。

有解决方案吗?

PS:我能够与Firebase联系。 注2:还安装了相关软件包。

链接到视频:

import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'dart:io';
import 'package:firebase_auth/firebase_auth.dart';
import 'dart:convert';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path_provider/path_provider.dart';
import 'package:ous/main.dart';

class kiban extends StatefulWidget {
  const kiban({Key? key}) : super(key: key);

  @override
  State<kiban> createState() => _kibanState();
}

class _kibanState extends State<kiban> {
  late Future<ListResult> futureFiles;

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

    futureFiles = FirebaseStorage.instance.ref('/files').list();
  }

  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(
          title: const Text('基盤教育科目'),
        ),
        body: FutureBuilder<ListResult>(
          future: futureFiles,
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              final files = snapshot.data!.items;

              return ListView.builder(
                itemCount: files.length,
                itemBuilder: (context, index) {
                  final file = files[index];

                  return ListTile(
                    title: Text(file.name),
                    trailing: IconButton(
                      icon: const Icon(
                        Icons.download,
                        color: Colors.black,
                      ),
                      onPressed: () => downloadFile(file),
                    ),
                  );
                },
              );
            } else if (snapshot.hasError) {
              return const Center(
                child: Text('error'),
              );
            } else {
              return const Center(
                child: CircularProgressIndicator(),
              );
            }
          },
        ),
      );

  Future downloadFile(Reference ref) async {
    final dir = await getApplicationDocumentsDirectory();
    final file = File('${dir.path}/${ref.name}');
    await ref.writeToFile(file);

    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text('ダウンロード中。ちょっと待ってね ${ref.name}')),
    );
  }



}

I am Japanese. I am using automatic translation, so please forgive me if my English is a little strange in places.

Now, my question is, I would like to link FirebaseStorage and Flutter to this video (link below) to get a list of files, but I am having trouble getting it.

Is there any solution?

PS: I am able to link with FireBase.
Note 2: FireBase related packages are also installed.

Link to video: https://www.youtube.com/watch?v=DzdOceMkGHw&t=70s

import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'dart:io';
import 'package:firebase_auth/firebase_auth.dart';
import 'dart:convert';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path_provider/path_provider.dart';
import 'package:ous/main.dart';

class kiban extends StatefulWidget {
  const kiban({Key? key}) : super(key: key);

  @override
  State<kiban> createState() => _kibanState();
}

class _kibanState extends State<kiban> {
  late Future<ListResult> futureFiles;

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

    futureFiles = FirebaseStorage.instance.ref('/files').list();
  }

  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(
          title: const Text('基盤教育科目'),
        ),
        body: FutureBuilder<ListResult>(
          future: futureFiles,
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              final files = snapshot.data!.items;

              return ListView.builder(
                itemCount: files.length,
                itemBuilder: (context, index) {
                  final file = files[index];

                  return ListTile(
                    title: Text(file.name),
                    trailing: IconButton(
                      icon: const Icon(
                        Icons.download,
                        color: Colors.black,
                      ),
                      onPressed: () => downloadFile(file),
                    ),
                  );
                },
              );
            } else if (snapshot.hasError) {
              return const Center(
                child: Text('error'),
              );
            } else {
              return const Center(
                child: CircularProgressIndicator(),
              );
            }
          },
        ),
      );

  Future downloadFile(Reference ref) async {
    final dir = await getApplicationDocumentsDirectory();
    final file = File('${dir.path}/${ref.name}');
    await ref.writeToFile(file);

    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text('ダウンロード中。ちょっと待ってね ${ref.name}')),
    );
  }



}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一江春梦 2025-02-20 01:58:21

请确保您可以从Firestorage中读写。
如果您的代码不允许您读取这些数据,则可以从Android Studio中的控制台(“底部运行页面”)获取一些相关信息。
因此,我们需要更改firebase存储的规则,要公开



后:

Please make sure you're able to read and write from Firestorage.
If your code does not allow you to read those data, you could get some related information from the console("RUN Page under bottom") in Android studio.
Therefore, we need to change the rules for Firebase Storage to be public, like this:

Before:
enter image description here
After:
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文