Advertisement
jevixlugya

Untitled

Apr 16th, 2024 (edited)
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 0.95 KB | None | 0 0
  1.   PlatformFile? pickedFile;
  2.  
  3.   UploadTask? uploadTask;
  4.   Future selectFile()async{
  5.     //final File fileForFirebase = File("${pickedFile!.path}");
  6.     final result= await FilePicker.platform.pickFiles();
  7.     if (result==null) return;
  8.     setState(() {
  9.       pickedFile=result.files.first;
  10.     });
  11.  
  12.     try {
  13.  
  14.       final path = 'postfiles/${pickedFile!.name}';
  15.       final file = File(pickedFile!.path!);
  16.       final ref = FirebaseStorage.instance.ref().child(path);
  17.       uploadTask = ref.putFile(file);
  18.       final imageUrl = await uploadTask!.whenComplete(() {});
  19.       final urlDownload = await imageUrl.ref.getDownloadURL();
  20.       print('firebase image =$urlDownload');
  21.  
  22.       setState(() {
  23.         postfileController.text = urlDownload;
  24.       });
  25.     }catch(e){
  26.     ScaffoldMessenger.of(context).showSnackBar(
  27.     const SnackBar(
  28.     content: Text(
  29.     'check your details /network, and try again',
  30.     ),
  31.     ),
  32.     );
  33.     }
  34.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement