RESTFUL API CALL
WITH HTTP
I CT425 MOBILE APPLICATION DEVELOPMENT
PRASHAYA FUSI RIPONG (PH.D.)
1. What is Asynchronous
Programming?
2. Why asynchronous code
matters?
OUTLINE 3. Future, async and await
4. HTTP GET Request in Flutter
5. HTTP POST Request in Flutter
6. Handling Error
WHAT IS ASYNCHRONOUS PROGRAMMING?
• Asynchronous programming
paradigm allows certain tasks to run
separately from the ‘main’
execution thread
The programs allowing your
application to continue executing
other tasks while waiting for the
asynchronous operation to finish
• Asynchronous events are
particularly useful in operations that
involve waiting such as network
requests, file I/O, or time-consuming
computations
WHY ASYNCHRONOUS CODE MATTERS?
• Asynchronous operations let your program complete work while
waiting for another operation to finish
• Key terms
Synchronous
- Operation: A synchronous operation blocks other operations from executing until it completes
- Function: A synchronous function only performs synchronous operations
Asynchronous
- Operation: Once initiated, an asynchronous operation allows other operations to execute before
it completes
- Function: An asynchronous function performs at least one asynchronous operation and can also
perform synchronous operations
FUTURE, ASYNC AND AWAIT
• Future is an object that represents the result of an asynchronous operation and
can have two states: uncompleted or completed
Then() returns the final result from the Future<TYPE> of Flutter, here <TYPE> means the
data type you want to return from the Future
catchError() UnCompleted or error data - do task when you didn’t able to get the
data or error occured
whenComplete() Executes on, either way, weather data was return or error was
thrown. Its used when you want to perform something in either case happens
• Async is used to make any function in Dart as an asynchronous process
Turn any function into an async function
the execution will perform in the background
• Await will wait for a future to complete before executing the subsequence
statement
CALLING DATA FROM SERVER REST API
CALLING DATA FROM SERVER REST API
• Dio package
A powerful HTTP networking package for Dart/Flutter, supports Global configuration,
Interceptors, FormData, Request cancellation, File uploading/downloading, Timeout,
Custom adapters, Transformers, etc.
• Installing Dio package
flutter pub add dio
This will add a line like this to your package's pubspec.yaml (and run an implicit dart
pub get):
dependencies:
dio: ^5.8.0+1
• Now in your Dart code, you can use: import 'package:dio/dio.dart';
HTTP GET REQUEST IN FLUTTER
HTTP POST REQUEST IN FLUTTER
HANDLING ERRORS
GETTING DATA FROM OPEN WEATHER API
• URL: https://openweathermap.org/
• Creating an Account from URL above
• There are many the weather API’s collection to get the weather data
Current & Forecast weather data collection
Solar Irradiance & Energy Prediction service
Historical weather data collection
Map collection
Other weather API’s collection (i.e. Air pollution and Fire weather Index API)
• Data format that use on the openweather API as Json data format
Thank you
Prashaya Fusiripong (Ph.D.)
Email: prashaya.f@rsu.ac.th
Department of Information and communication technology (ICT), International college