是否有一种方法可以列出Firebase Cloud存储中文件夹的名称并在扑朔迷离中显示它们(例如,列表视图)

发布于 2025-02-12 05:09:52 字数 3641 浏览 0 评论 0原文

假设您的文件是这样构成的:文件夹,然后是子文件夹列表,您将如何访问这些子文件夹的名称,并可能在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 技术交流群。

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

发布评论

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

评论(1

拍不死你 2025-02-19 05:09:52

这个工作吗?

final storageRef = FirebaseStorage.instance.ref().child("front end");
final listResult = await storageRef.listAll();

// for (var prefix in listResult.prefixes) {
  // The prefixes under storageRef.
  // You can call listAll() recursively on them.
// }

for (var item in listResult.items) {
  // The items under storageRef.
}

https://firebase.google.com/docs/docs/docs/storage/storage/flutter/list/list/list-/list-list-com-文件

Does this work?

final storageRef = FirebaseStorage.instance.ref().child("front end");
final listResult = await storageRef.listAll();

// for (var prefix in listResult.prefixes) {
  // The prefixes under storageRef.
  // You can call listAll() recursively on them.
// }

for (var item in listResult.items) {
  // The items under storageRef.
}

https://firebase.google.com/docs/storage/flutter/list-files

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