我想从firebase存储中获取下载URL

发布于 2025-02-02 23:47:54 字数 5025 浏览 1 评论 0原文

这是我从Firebase存储中获取URL并显示图像的构建代码。图像名称保存在我的Firestore上,作为数组。因此,我需要获取图像名称的列表,并将其转换为Firestorage的URL列表。

@override
  Widget build(BuildContext context){

  return Container(
      child: Stack(
        children: [
          FutureBuilder<QuerySnapshot>(
              future: ref.get(),
              builder: (context, snapshot) {
                if(snapshot.hasError){
                  return Scaffold(
                    body: Center(
                      child: Text("Error: ${snapshot.error}"),
                    ),
                  );
                }

                if(snapshot.connectionState == ConnectionState.done){
                  return Scaffold(
                    appBar: AppBar(title: Text("Home"),
                      centerTitle: true,
                    ),
                    body: GridView(gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
                        maxCrossAxisExtent: 200,
                        childAspectRatio: 2/3,
                        crossAxisSpacing: 15,
                        mainAxisSpacing: 15),
                      children: snapshot.data!.docs.map((document){
                        DateTime dt = (document['endDateTime'] as Timestamp).toDate();
                        List im =   DownloadURL(document['imageURL']);// this are the call method
                        
                        return Card(
                          clipBehavior: Clip.antiAlias,
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(15),
                          ),
                          child:Column(
                            children: [
                              Stack(
                                alignment: Alignment.center,
                                children: [
                                  Ink.image(
                                    image: NetworkImage(im[0]),
                                    child: InkWell(
                                      onTap: (){
                                        Navigator.of(context).push(MaterialPageRoute(builder: (context)=>DetailScreen(productId: document.id,)));
                                      },
                                    ),
                                    height: 170,
                                    fit: BoxFit.cover,
                                  ),

                                ],
                              ),

                              Expanded(
                                child: Container(
                                  alignment: Alignment.center,
                                  child: Padding(padding: EdgeInsets.all(4).copyWith(bottom: 0),
                                    child: Text("${document['nameP']}",
                                      style: TextStyle(fontSize: 16),
                                    ),
                                  ),
                                ),
                              ),
                              // SizedBox(height: 3),
                              Expanded(
                                child: Container(
                                  alignment: Alignment.center,
                                  child: Padding(padding: EdgeInsets.all(6).copyWith(bottom: 0),
                                    child: Text("RM ${document['startPrice']}",
                                      style: TextStyle(fontSize: 16),
                                    ),
                                  ),
                                ),
                              ),
                              SizedBox(height: 6),
                              Expanded(child: CountDownText(due: dt,
                                finishedText: "The Auction has End",
                                showLabel: true,
                                daysTextShort: "D ",
                                hoursTextShort: "H ",
                                minutesTextShort: "M ",
                                secondsTextShort: "S ",
                                style: TextStyle(fontSize: 16,color: Colors.deepPurpleAccent),
                              ),
                              ),
                              SizedBox(height: 2),
                            ],
                          ),
                        );
                      }).toList(),
                    ),
                  );


                }

                //loading State
                return Scaffold(
                  body: Center(
                    child: CircularProgressIndicator(),
                  ),
                );
              }

          ),
        ],
      ),
  );
}

这是我从firebase存储中获取下载URL的方法

 DownloadURL(List images) async{
  List url = [];
  for(int i = 0; i<images.length; i++){
    url.add(await st.ref('products/${images[i]}').getDownloadURL());
  }
  return url;
}

,它将显示错误,在运行代码时,键入“未来”不是类型“列表”的子类型。

here is my build code for getting the URL from firebase storage and showing the image. The image name is save on my firestore as array. So i need to get the list of the image name and turn it into list of URL from firestorage.

@override
  Widget build(BuildContext context){

  return Container(
      child: Stack(
        children: [
          FutureBuilder<QuerySnapshot>(
              future: ref.get(),
              builder: (context, snapshot) {
                if(snapshot.hasError){
                  return Scaffold(
                    body: Center(
                      child: Text("Error: ${snapshot.error}"),
                    ),
                  );
                }

                if(snapshot.connectionState == ConnectionState.done){
                  return Scaffold(
                    appBar: AppBar(title: Text("Home"),
                      centerTitle: true,
                    ),
                    body: GridView(gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
                        maxCrossAxisExtent: 200,
                        childAspectRatio: 2/3,
                        crossAxisSpacing: 15,
                        mainAxisSpacing: 15),
                      children: snapshot.data!.docs.map((document){
                        DateTime dt = (document['endDateTime'] as Timestamp).toDate();
                        List im =   DownloadURL(document['imageURL']);// this are the call method
                        
                        return Card(
                          clipBehavior: Clip.antiAlias,
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(15),
                          ),
                          child:Column(
                            children: [
                              Stack(
                                alignment: Alignment.center,
                                children: [
                                  Ink.image(
                                    image: NetworkImage(im[0]),
                                    child: InkWell(
                                      onTap: (){
                                        Navigator.of(context).push(MaterialPageRoute(builder: (context)=>DetailScreen(productId: document.id,)));
                                      },
                                    ),
                                    height: 170,
                                    fit: BoxFit.cover,
                                  ),

                                ],
                              ),

                              Expanded(
                                child: Container(
                                  alignment: Alignment.center,
                                  child: Padding(padding: EdgeInsets.all(4).copyWith(bottom: 0),
                                    child: Text("${document['nameP']}",
                                      style: TextStyle(fontSize: 16),
                                    ),
                                  ),
                                ),
                              ),
                              // SizedBox(height: 3),
                              Expanded(
                                child: Container(
                                  alignment: Alignment.center,
                                  child: Padding(padding: EdgeInsets.all(6).copyWith(bottom: 0),
                                    child: Text("RM ${document['startPrice']}",
                                      style: TextStyle(fontSize: 16),
                                    ),
                                  ),
                                ),
                              ),
                              SizedBox(height: 6),
                              Expanded(child: CountDownText(due: dt,
                                finishedText: "The Auction has End",
                                showLabel: true,
                                daysTextShort: "D ",
                                hoursTextShort: "H ",
                                minutesTextShort: "M ",
                                secondsTextShort: "S ",
                                style: TextStyle(fontSize: 16,color: Colors.deepPurpleAccent),
                              ),
                              ),
                              SizedBox(height: 2),
                            ],
                          ),
                        );
                      }).toList(),
                    ),
                  );


                }

                //loading State
                return Scaffold(
                  body: Center(
                    child: CircularProgressIndicator(),
                  ),
                );
              }

          ),
        ],
      ),
  );
}

and here is my method for getting the download URL from firebase storage

 DownloadURL(List images) async{
  List url = [];
  for(int i = 0; i<images.length; i++){
    url.add(await st.ref('products/${images[i]}').getDownloadURL());
  }
  return url;
}

it will show error, type 'Future' is not a subtype of type 'List' when run the code.

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

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

发布评论

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

评论(1

意中人 2025-02-09 23:47:54

downloadurl应返回list

Future<List<String>> DownloadURL(List images) async {
...
}

参见 asynchronous编程

DownloadURL should return List.

Future<List<String>> DownloadURL(List images) async {
...
}

See Asynchronous programming

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