Android App Development
iOS App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 18.104.22.168 release ready!
Owner and Founder. Opencart
In the mobile app development lifecycle, communicating with the server(via API) to fetch or store data is one of the basic needs.
So, in today’s blog let’s check Retrofit API calling in a flutter.
I hope you’re all familiar with Android/iOS, and you’ve even heard of Retrofit and Alamofire.
We use network calls in almost every app, and they’ve become an integral part of app functioning.
To call Rest APIs by sending dynamic headers, parameters, print requests, and responses in a more custom and secured way “Retrofit” is the best solution.
Step1: Create a flutter project.
Step2: Add the below dependencies in pubspec.yaml file
Now, run the command “flutter pub get” to download the dependencies.
Step3: Now, let’s create an abstract API request class.
Right now, you will observe some errors in the file in “part ‘api_client.g.dart’ ” and _ApiClient.
We need to run a command and generate an “api_client.g.dart” file.
Step4: Now run the command in the terminal
flutter pub run build_runner build
The above command will generate the api_client.g.dart file & contains all data about creating Retrofit instance and fetching data from the network.
You should never modify the code in api_client.g.dart file by hand.
If you made any changes in ApiClient then also run the command to update part file.
Step5: Now, create a model class to store the API data.
Step6: Then, call the get method from your main.dart class.
Please find below the complete code for main.dart class.
Finally, you can run the code, and see the below results.
For more details and methods you can refer to the doc here.
For more interesting blogs check out here – https://mobikul.com/blog/
Hope this blog helped you for a better understanding of Retrofit API calling in flutter.
Thanks for reading 🙂
Your email address will not be published. Required fields are marked*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
Webkul is a truly trusted and supported IT Company to develop business ideas for e-commerce. They provide professional support after deploying the solution to production and be responsible to act for fixing the reported issues or system errors. We highly encourage to deal with Webkul for business development.
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.