是否有一种方法可以列出Firebase Cloud存储中文件夹的名称并在扑朔迷离中显示它们(例如,列表视图)
假设您的文件是这样构成的:文件夹,然后是子文件夹列表,您将如何访问这些子文件夹的名称,并可能在listView中显示它们。我尝试了“结果。任何潜在客户或资源将不胜感激。
所以@dabbel我尝试了您的建议,并做到了。正确的地方,我错了,现在我陷入了“ LationInitializationerror Field'ListreSult”尚未被初始化。以下是我的代码: 模型文件:
class FirebaseFolder {
final Reference ref;
final String name;
final String url;
const FirebaseFolder({
required this.ref,
required this.name,
required this.url,
});
}
屏幕代码:
class DashBoard extends StatefulWidget {
const DashBoard({Key? key}) : super(key: key);
@override
State<DashBoard> createState() => _DashBoardState();
}
class _DashBoardState extends State<DashBoard> {
late List<String> folderYear = [];
late Future<List<FirebaseFolder>> listResult;
@override
void initState() async{
super.initState();
await getFolders();
}
Future<List<String>> getFolders() async {
final storageRef = FirebaseStorage.instance.ref().child("front end");
final listResult = await storageRef.listAll();
for (var prefix in listResult.prefixes) {
folderYear.add(prefix.name);
}
return folderYear;
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: kAppBar('DashBoard'),
body: FutureBuilder<List<FirebaseFolder>>(
//future: futureFiles,
future: listResult,
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return const Center(
child: spinkitLines,
);
default:
// final files = snapshot.data;
final folders = snapshot.data;
return Column(
children: [
const SizedBox(
height: 30.0,
),
const Center(
child: Text(
'Tap to choose semester',
style: kPlaceholderStyle,
),
),
const SizedBox(
height: 30,
),
Expanded(
child: ListView.builder(
itemCount: folderYear.length,
itemBuilder: (context, index) {
final folder = folders![index];
return Container(
height: 80,
decoration: const BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.all(Radius.circular(20)),
boxShadow: [
BoxShadow(
color: Color.fromARGB(75, 0, 0, 0),
blurRadius: 4,
spreadRadius: 0,
offset: Offset(0, 4))
],
),
child: Center(
child: Padding(
padding: const EdgeInsets.all(5.0),
child: Text(
folder.name,
style: kCardTitleStyle,
),
),
),
);
},
),
)
],
);
}
},
),
);
}
}
Let's say your files are structured like this: folder then a list of subfolders, how would you access the names of those subfolders and maybe display them in a listview. I have tried "result.prefixes" but am having trouble implementing it. Any leads or resources will be appreciated.
So @Dabbel I tried what you suggested and did this. Correct where i am wrong, right now i'm stuck at "lateinitializationerror field 'listResult' has not been initialized". Below is my code:
The models file:
class FirebaseFolder {
final Reference ref;
final String name;
final String url;
const FirebaseFolder({
required this.ref,
required this.name,
required this.url,
});
}
The screen code:
class DashBoard extends StatefulWidget {
const DashBoard({Key? key}) : super(key: key);
@override
State<DashBoard> createState() => _DashBoardState();
}
class _DashBoardState extends State<DashBoard> {
late List<String> folderYear = [];
late Future<List<FirebaseFolder>> listResult;
@override
void initState() async{
super.initState();
await getFolders();
}
Future<List<String>> getFolders() async {
final storageRef = FirebaseStorage.instance.ref().child("front end");
final listResult = await storageRef.listAll();
for (var prefix in listResult.prefixes) {
folderYear.add(prefix.name);
}
return folderYear;
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: kAppBar('DashBoard'),
body: FutureBuilder<List<FirebaseFolder>>(
//future: futureFiles,
future: listResult,
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return const Center(
child: spinkitLines,
);
default:
// final files = snapshot.data;
final folders = snapshot.data;
return Column(
children: [
const SizedBox(
height: 30.0,
),
const Center(
child: Text(
'Tap to choose semester',
style: kPlaceholderStyle,
),
),
const SizedBox(
height: 30,
),
Expanded(
child: ListView.builder(
itemCount: folderYear.length,
itemBuilder: (context, index) {
final folder = folders![index];
return Container(
height: 80,
decoration: const BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.all(Radius.circular(20)),
boxShadow: [
BoxShadow(
color: Color.fromARGB(75, 0, 0, 0),
blurRadius: 4,
spreadRadius: 0,
offset: Offset(0, 4))
],
),
child: Center(
child: Padding(
padding: const EdgeInsets.all(5.0),
child: Text(
folder.name,
style: kCardTitleStyle,
),
),
),
);
},
),
)
],
);
}
},
),
);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个工作吗?
https://firebase.google.com/docs/docs/docs/storage/storage/flutter/list/list/list-/list-list-com-文件
Does this work?
https://firebase.google.com/docs/storage/flutter/list-files