Networking
Before we deep dive into networking, you must familiarize yourself with build_runner and dependency injection because this starter heavily use build_runner for code generation.
Example Case:
Request :
GET https://example-api.com/api/user/1
Response :
Json :
{
"message":"success",
"data": {
"id":1,
"name":"dash"
}
}Open command line and run build_runner in watch mode
flutter pub run build_runner watch --delete-conflicting-outputsOpen core/config/config.dart and set BASE_URL to https://example-api.com/api
class Config {
static const baseUrl = 'https://example-api.com/api';
}Create generic class to handle common response pattern.
Create model class and annotate it with JsonSerializable to parse json data. And don't forget to extend Equatable for value equality
Create abstract class to automatically generate network implementation class.
And finally you can get data from network like this :
You can read retrofit documentation here
Last updated