Skip to content

Route Path Practices

Veli Bacik edited this page Sep 25, 2020 · 1 revision

I have seen network request URL wrote static in the function to many projects. It's a simple way but unsustainable. You can use route path enum with an extension so that you will use without wrote static URL

You create a network_route file and write your route paths string with raw value extension.

enum RoutePath { LOGIN, PRODUCT_BEST_SELL}

extension RoutePathExtension on RoutePath {
  String get rawValue {
    switch (this) {
      case RoutePath.LOGIN:
        return '/login/accessToken';

      case RoutePath.PRODUCT_BEST_SELL:
        return '/product/best';

      default:
        throw "Route Path does not EMPTY!";
    }
  }
}

For example:

 final response = await networkManager.fetch<Product, List<Product>>(RoutePath.PRODUCT_BEST_SELL.rawValue,
        parseModel: Product(), method: RequestType.GET);

You should use route path enum and extension same file. If you want to separate this, you must import RoutePathExtension wherever you use

Clone this wiki locally