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"
    }
}
  1. Open command line and run build_runner in watch mode

flutter pub run build_runner watch --delete-conflicting-outputs
  1. Open core/config/config.dart and set BASE_URL to https://example-api.com/api

class Config {
  static const baseUrl = 'https://example-api.com/api';
}
  1. Create generic class to handle common response pattern.

  1. Create model class and annotate it with JsonSerializable to parse json data. And don't forget to extend Equatable for value equality

  1. Create abstract class to automatically generate network implementation class.

  1. And finally you can get data from network like this :

You can read retrofit documentation herearrow-up-right

Last updated