0

I am using Future builder inside a stream builder to get audio and image file from firebase storage but its always giving me null here is the code: This is the function:

Future <List<String>> getFiles(List<String> files) async {
  
  List<String> urls = [];
  String img = "abc";
  String sound = "abc";
  try {
    files.forEach((file) async{
      urls.add(await FirebaseStorage.instance.ref().child(file).getDownloadURL());
    });
    return urls;
  } catch (e) {
    print(e);
    return ["null"];
  }
}

And here is the stream builder

return StreamBuilder(
    stream: FirebaseFirestore.instance.collection("users")
        .doc("LQ0PFtnsaxXU1c4tY0ZM")
        .collection("qr_details")
        .where('animal_id', isEqualTo: animal_id_from_qr )
        .snapshots(),
    builder: (context, AsyncSnapshot<QuerySnapshot> streamSnapshot) {
      // List<Widget> Data = [];
      // var image_2;
      if(streamSnapshot.connectionState == ConnectionState.waiting){
        return Center(child: CircularProgressIndicator());
      }
      final animal_data = streamSnapshot.data?.docs;
      return animal_data?.length != 0
          ? Column(children: [
        FutureBuilder<List<String>>(future: getFiles(
            [
              "qrdetails/${animal_data![0]["animal_image"]}" ,
              "qrdetails/${animal_data![0]["animal_sound"]}"
            ]
        )
            ,builder: (_, imageSnapshot){
              var data_animal = imageSnapshot.data;
              Fluttertoast.showToast(msg: data_animal.toString());
              String? imageURL = imageSnapshot.data?[0];
              Fluttertoast.showToast(msg: imageURL);
              //String? soundURL = imageSnapshot.data![1];
              return imageURL != null
                  ? Container(
                child:Center(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      SizedBox(height: 20,),
                      Container(
                        width: 200,
                        height: 200,
                        decoration: BoxDecoration(
                          shape: BoxShape.circle,
                          image: DecorationImage(
                              image: NetworkImage(imageURL),
                              fit: BoxFit.fill
                          ),
                        ),
                      ),
                      SizedBox(height: 20,),
                      CupertinoButton(
                        color: Colors.teal[800],
                        child: const Text("Play" ,
                          style: TextStyle(
                              color: Colors.white,
                              fontWeight: FontWeight.bold
                          ),),
                        onPressed: () {

                        },
                      ),
                    ],
                  ),
                ),
              )
                  :  Loading_Page();
            })
      ])
          : Column();
    });

I tried to get both the image and audio file in a single future builder to display the data but it is not working

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

0 Answers0