我想在flutter中使用firebasestorage获取文件列表,但是我可以
我是日本人。我正在使用自动翻译,所以如果我的英语在某些地方有些奇怪,请原谅我。
现在,我的问题是,我想将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}')),
);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请确保您可以从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:


After: