Flutter : Download Api Request | Download a file using Dio library | amplifyabhi

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this Vlog we are going to deal with the  implementation of an API request using which   we are going to download a file so till now  we have done API request for cloud operations   making use of a do library so we  have performed get post put delete   update so all this API requests we  have seen in our previous tutorials   using Dio library and in this part of the tutorial  we are going to download a text file with the help   of an API request so if you are watching this  tutorial for the first time I suggest you to go   through our previous tutorials on Dio Library  implementation in flutter application so that   you will be getting a basic knowledge on how  we Implement a network call in our flutter   applications using to library so now let's get  started with this particular tutorial foreign so the very first thing I would like to  change here is like we are making use of   the previous tutorials code so we'll be  just trying to edit this particular get   data method to download our file so the very  first thing is let's change it to download file   and then the method name also I  am going to change it to download   file and you need to appropriately change this  even here so that whenever we tap on the pattern   we are able to call this particular method  and our download API request is been called   so we have initialized Dio Library here and  created an object so that we can make use of   the so we can specify our API request and as we  are going to download a file here so we need to   specify the exact path and location where we want  to download our file and for which let us try to   make use of a directory here and create an object  for it so that we can get the absolute path where   we want to download our text file so add a weight  here so that we need to find the location and try   to save it in this particular object and later  we can use it so application document directory   so now we are going to fetch the location  and trying to use it to save our text file   and here instead of get here we are having  multiple options now so to dot we have got   multiple options like delete download and fetch  get these things we have seen in our previous   tutorials so now we are going to use download  here and the download API request will accept so   the download API request will accept two inputs  here so the first input is the API link here   so the link for our file we need to specify it  over here and this is the link I am providing   now so from filesamples.com we are going  to download this sample 1.txt file and now   the second thing we need to specify here  is the place where we want to store this   so this is also an important step now you need  to specify the exact location where you want to   download the file just try to carefully observe  this and how I am going to provide the location   so we are going to make use of the directory  here we need to make use of the directory here   so I am specifying directory dot path and we  need to also provide the name here so the file   name also we need to provide here what what is  the name of the file using which we are going   to store the what is the name of the file by  using which we are going to store it so I am   just trying to specify file dot txt here and  if You observe here in the URL we have got the   name to be sample1.txt but now we are storing  it on our device with the name called file.txt   but the data inside the file will be same just  we are trying to provide the name according to   our application requirement that's all there is  no change in the file name you can just specify   any name here according to your requirement and  now let's try to run this particular code and   see the output once and also one more thing  I would like to specify here is like we are   downloading the file here and we need to get the  user permission to write user permission in terms   of write permissions so I have dealed clearly  the implementation of runtime permissions in   this particular tutorial so I suggest you to  go through this particular tutorial and if you   are still having any queries do let me know in the  comment section below like how we have implemented   the runtime permissions so then I would surely  help you with it and now here let's try to tap   on the download file button and try to observe  the console like what the thing is going on   so here you can see we have got the response dot  status code AS 200 so if you have gone through our   previous tutorials you might be well aware like  what is the status code and if it is 200 then   indicate indicates that this API call is success  and now let's try to see where our file is been   downloaded so for which let's go to the device  file explorer here and here you can see we have   got file.txt file under our package name here so  when you tap on this particular file you can see   like we have got the same file loaded and now  for your observation let's try to rename this   particular file to file 2.txt and now let's try  to run this particular code and see the output   once and when we tap on download file button  here we again got status code 200 that is our   API call is success but now here you can see our  file is not being displayed here and still it is   showing file.txt so this might be a confusion for  the beginners and but don't get confused in this   particular step here just try to refresh your  emulator and try to go through the data folder   again and inside the data folder there is again a  data folder and from there you need to choose your   package name here so my package name is like com  dot example dot flutter underscore do here so here   inside which in app underscore flutter folder we  are storing our file and now here clearly you can   see our file 2.txt is also been downloaded here  so don't get confused when you don't see your file   they just try to refresh the device file explorer  then surely you can see the downloaded file again and if you are having any queries regarding the  path of this particular folder where you are   trying to store the data I suggest you to just try  to print the directory dot path here so just try   to print this directory.path with the help of  a print statement and you will surely and you   will get to know the exact path in the console  where you are trying to store your file here   and this file name also you can just dynamically  take from the user by just specifying an input   label here so in our previous tutorials you can  just try to see here how we have provided an input   label and try to accept the user information so  in that way you can just generate the file name   here and also you can just Auto generate the file  names by making use of any other random scenarios   so yes this is it for this tutorial and regarding  the permissions I suggest you to go to this   particular tutorial on how to seek the runtime  permission and if you are having still query just   let me know in the comment section below I will  surely try to help you and this is it for this   tutorial on download API request implementation  using Duo library in your flutter application   and the code for this tutorial is provided  in the description section below if you like   this tutorial do like share and subscribe to our  channel for more interesting content on flutter
Info
Channel: amplifyabhi coding
Views: 5,550
Rating: undefined out of 5
Keywords:
Id: TJU_-ZNl64Q
Channel Id: undefined
Length: 8min 52sec (532 seconds)
Published: Tue Oct 18 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.