Skip to content

Commit 504c981

Browse files
committed
feat: Added example network request with retrofit
1 parent 75e9207 commit 504c981

29 files changed

+338
-129
lines changed

.vscode/launch.json

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,44 @@
11
{
2-
// Use IntelliSense to learn about possible attributes.
3-
// Hover to view descriptions of existing attributes.
4-
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5-
"version": "0.2.0",
6-
"configurations": [
7-
{
8-
"name": "Development",
9-
"request": "launch",
10-
"type": "dart",
11-
"program": "lib/main_development.dart",
12-
"args": [
13-
"--flavor",
14-
"development"
15-
]
16-
},
17-
{
18-
"name": "Staging",
19-
"request": "launch",
20-
"type": "dart",
21-
"program": "lib/main_staging.dart",
22-
"args": [
23-
"--flavor",
24-
"staging"
25-
]
26-
},
27-
{
28-
"name": "Production",
29-
"request": "launch",
30-
"type": "dart",
31-
"program": "lib/main_production.dart",
32-
"args": [
33-
"--flavor",
34-
"production"
35-
]
36-
},
37-
]
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Development",
9+
"request": "launch",
10+
"type": "dart",
11+
"program": "lib/main_development.dart",
12+
"args": [
13+
"--flavor",
14+
"development",
15+
"--dart-define",
16+
"API_BASE_URL=https://randomuser.me"
17+
]
18+
},
19+
{
20+
"name": "Staging",
21+
"request": "launch",
22+
"type": "dart",
23+
"program": "lib/main_staging.dart",
24+
"args": [
25+
"--flavor",
26+
"staging",
27+
"--dart-define",
28+
"API_BASE_URL=https://randomuser.me"
29+
]
30+
},
31+
{
32+
"name": "Production",
33+
"request": "launch",
34+
"type": "dart",
35+
"program": "lib/main_production.dart",
36+
"args": [
37+
"--flavor",
38+
"production",
39+
"--dart-define",
40+
"API_BASE_URL=https://randomuser.me"
41+
]
42+
}
43+
]
3844
}

.vscode/ml-flutter.code-snippets

Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,63 @@
11
{
2-
"Freezed Class": {
3-
"prefix": "freezed",
4-
"body": [
5-
"import 'package:freezed_annotation/freezed_annotation.dart';",
6-
"",
7-
"part '${1:file}.freezed.dart';",
8-
"part '${1:file}.g.dart';",
9-
"",
10-
"@freezed",
11-
"class ${2:ClassName} with _$${2:ClassName} {",
12-
" const factory ${2:ClassName}({",
13-
" required ${0:param},",
14-
" }) = _${2:ClassName};",
15-
"",
16-
" factory ${2:ClassName}.fromJson(Map<String, dynamic> json) =>",
17-
" _$${2:ClassName}FromJson(json);",
18-
"}"
19-
],
20-
"description": "Creates basement for a freezed class."
21-
},
22-
}
2+
"Remote Data Source": {
3+
"prefix": "remote_data_source",
4+
"description": "Snippet for quick creation of a remote data source.",
5+
"body": [
6+
"import 'package:dio/dio.dart';",
7+
"import 'package:flutter_template/injection/network_module.dart';",
8+
"import 'package:injectable/injectable.dart';",
9+
"import 'package:retrofit/retrofit.dart';",
10+
"",
11+
"part '$TM_FILENAME_BASE.g.dart';",
12+
"",
13+
"@RestApi()",
14+
"@lazySingleton",
15+
"abstract class ${2:Scope}RemoteDataSource {",
16+
" @factoryMethod",
17+
" factory $2RemoteDataSource(",
18+
" @Named(dioAuthenticated) Dio dio,",
19+
" ) = _$2RemoteDataSource;",
20+
"",
21+
" ${3:/* Fill your calls here */}",
22+
"}",
23+
""
24+
]
25+
},
26+
"Freezed Class": {
27+
"prefix": "freezed",
28+
"body": [
29+
"import 'package:freezed_annotation/freezed_annotation.dart';",
30+
"",
31+
"part '${1:file}.freezed.dart';",
32+
"part '${1:file}.g.dart';",
33+
"",
34+
"@freezed",
35+
"class ${2:ClassName} with _$${2:ClassName} {",
36+
" const factory ${2:ClassName}({",
37+
" required ${0:param},",
38+
" }) = _${2:ClassName};",
39+
"",
40+
" factory ${2:ClassName}.fromJson(Map<String, dynamic> json) =>",
41+
" _$${2:ClassName}FromJson(json);",
42+
"}"
43+
],
44+
"description": "Creates basement for a freezed class."
45+
},
46+
"Remapper": {
47+
"prefix": "remapper",
48+
"description": "Create a basement for a remapper class.",
49+
"body": [
50+
"import 'package:injectable/injectable.dart';",
51+
"",
52+
"@lazySingleton",
53+
"class ${1:Entity}Remapper {",
54+
" ${1:Entity}Entity fromResponse(${1:Entity}Response response) {",
55+
" return ${1:Entity}Entity(",
56+
" ${2}",
57+
" );",
58+
" }",
59+
"",
60+
"}"
61+
]
62+
}
63+
}

build.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ targets:
1212
include:
1313
# data layer:
1414
- lib/data/model/**.dart
15+
- lib/data/services/**_request.dart
16+
- lib/data/services/**_response.dart
1517
- lib/data/response_objects/**.dart
1618

1719
# domain layer:
@@ -20,13 +22,21 @@ targets:
2022
# We should not allow the entire `/entities` folder here, as entities don't need fromJson(), toJson(), etc.
2123
# Specifying exact file will help build runner to run fast.
2224
- lib/domain/entities/user.dart
25+
26+
# Retrofit Classes / Remote Data Sources
27+
retrofit_generator:
28+
generate_for:
29+
include:
30+
- lib/data/**/data_sources/remote/*_remote_data_source.dart
2331

2432
# Data Classes, Cloning
2533
freezed:freezed:
2634
generate_for:
2735
include:
2836
# data layer:
2937
- lib/data/model/**.dart
38+
- lib/data/services/**_request.dart
39+
- lib/data/services/**_response.dart
3040
- lib/data/response_objects/**.dart
3141

3242
# domain layer:
@@ -46,9 +56,12 @@ targets:
4656
generate_for:
4757
include:
4858
- lib/injection/injector.dart
59+
- lib/injection/modules/*_module.dart
4960

5061
# data
5162
- lib/data/**_config.dart
63+
- lib/data/**_client.dart
64+
- lib/data/**/data_sources/remote/*_remote_data_source.dart
5265
- lib/data/services/**_service_impl.dart
5366
- lib/data/services/**_remapper.dart
5467
- lib/data/preferences/**_preferences.dart

lib/data/api/api_config.dart

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import 'package:injectable/injectable.dart';
2-
3-
@Injectable()
1+
/// A configuration class providing properties specific to an API.
2+
///
3+
/// Class instance is registered via [ApiConfigModule].
44
class ApiConfig {
5-
final String baseUrl;
5+
ApiConfig({
6+
required this.baseUrl,
7+
});
68

7-
ApiConfig(this.baseUrl);
9+
final String baseUrl;
810

911
String get apiUrl => '$baseUrl/api';
1012
}

lib/data/services/http_client/dio_http_client.dart renamed to lib/data/core/http_client/dio_http_client.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import 'package:dio/dio.dart';
2-
import 'package:flutter_template/data/response_objects/response_error.dart';
3-
import 'package:flutter_template/data/services/http_client/http_client.dart';
2+
import 'package:flutter_template/domain/common/response_error/response_error.dart';
3+
import 'package:flutter_template/data/core/http_client/http_client.dart';
4+
import 'package:flutter_template/injection/modules/network_module.dart';
45
import 'package:injectable/injectable.dart';
56

67
/// Abstraction of the Dio http client class.
78
89
@Injectable(as: HttpClient)
910
class DioHttpClient extends HttpClient {
10-
DioHttpClient(this.dio);
11+
DioHttpClient(@Named(dioForAuthentication) this.dio);
1112

1213
final Dio dio;
1314

lib/data/services/http_client/dio_http_client_builder.dart renamed to lib/data/core/http_client/dio_http_client_builder.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import 'package:dio/dio.dart';
2-
import 'package:flutter_template/data/services/http_client/dio_http_client.dart';
2+
import 'package:flutter_template/data/core/http_client/dio_http_client.dart';
33

44
DioHttpClient? _dioClient;
55

lib/data/interceptor/auth_interceptor.dart renamed to lib/data/interceptors/auth_interceptor.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import 'package:dio/dio.dart';
22
import 'package:flutter/foundation.dart';
3-
import 'package:flutter_template/data/interceptor/meta_interceptor.dart';
3+
import 'package:flutter_template/data/interceptors/meta_interceptor.dart';
44
import 'package:flutter_template/data/model/auth/auth_tokens.dart';
55
import 'package:flutter_template/data/preferences/auth_preferences.dart';
6-
import 'package:flutter_template/data/response_objects/response_error.dart';
7-
import 'package:flutter_template/data/services/http_client/dio_http_client.dart';
8-
import 'package:flutter_template/data/services/http_client/http_client.dart';
6+
import 'package:flutter_template/domain/common/response_error/response_error.dart';
7+
import 'package:flutter_template/data/core/http_client/dio_http_client.dart';
8+
import 'package:flutter_template/data/core/http_client/http_client.dart';
99
import 'package:flutter_template/data/response_objects/tokens_response.dart';
1010
import 'package:flutter_template/domain/preferences/user_preferences.dart';
1111

12-
class AuthInterceptor extends InterceptorsWrapper {
12+
class AuthInterceptor extends QueuedInterceptor {
1313
AuthInterceptor({
1414
required this.httpClient,
1515
required this.onTokenExpired,

lib/data/interceptor/meta_interceptor.dart renamed to lib/data/interceptors/meta_interceptor.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import 'package:flutter/foundation.dart';
44
import 'package:flutter_template/presentation/app_flavor.dart';
55
import 'package:package_info_plus/package_info_plus.dart';
66

7-
class MetaInterceptor extends InterceptorsWrapper {
7+
class MetaInterceptor extends QueuedInterceptor {
88
MetaInterceptor(this.flavor);
99

1010
static String nMetaHeaderKey = 'n-meta';

lib/data/preferences/user_shared_preferences.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import 'package:shared_preferences/shared_preferences.dart';
77

88
/// Store the current authenticated user's basic information.
99
10-
@LazySingleton()
11-
class UserSharedPreferences extends UserPreferences {
10+
@LazySingleton(as: UserPreferences)
11+
class UserSharedPreferences implements UserPreferences {
1212
UserSharedPreferences(this._preferences);
1313

1414
final SharedPreferences _preferences;

0 commit comments

Comments
 (0)