'Upload image or anyother file to Firebase and Realtime-Database using HTTP in flutter for web

I am working on storing files to Firebase Storage in Flutter but I have made it for mobile app but it doesn't really work for Web. please help me solve this problem of how I can store files to Firebase Storage for web in flutter.

Thanks


class _CheckingUploadingFilesState extends State<CheckingUploadingFiles> {
  @override
  Widget build(BuildContext context) {
    final filename = file != null ? basename(file.path) : 'No File Selected';
    return Scaffold(
      body: Container(
        child: Column(
          children: [
            InkWell(onTap: () {
              selectFile();
            },child: Container(padding: EdgeInsets.all(20),color: Colors.blueAccent,child: Text("Select File"))),
            SizedBox(height: 30,),
            Container(child: Center(child: Text(filename))),
            SizedBox(height: 30,),
            InkWell(
              onTap: () {
               uploadFile();
              },
              child: Container(padding: EdgeInsets.all(20),color: Colors.blueAccent,child: Text("Upload File"))),
          ],
        ),
      ),
    );
  }


  File file;

  Future selectFile() async {
    final result = await FilePicker.platform.pickFiles(allowMultiple: false);
    if (result == null) return;
    final path = result.files.single.path;
    print("Image : "+path.toString());
    setState(() => file = File(path));
  }

  Future uploadFile() async {
    if (file == null) return;

    DatabaseReference ref = FirebaseDatabase.instance.reference();
    String Filename = ref.push().key;
    final fileName = Filename;
    try {
      final Sref = FirebaseStorage.instance.refFromURL("gs://projectone.appspot.com").child("apps").child("369").child(fileName);
      return Sref.putFile(file);
    } on FirebaseException catch (e) {
      return null; 
    }
  }

}



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source