This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "2.0.0", | |
"tasks": [ | |
{ | |
"label": "flutter: analyze", | |
"type": "shell", | |
"command": "flutter", | |
"group": "build", | |
"runOptions": { | |
"instanceLimit": 1, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Model interface | |
abstract class JsonConvertible { | |
Map<String, dynamic> toJson(); | |
JsonConvertible.fromJson(Map<String, dynamic> json); | |
} | |
// Model | |
class AccessTokenResponse extends JsonConvertible { | |
String? accessToken; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final dio = Dio() | |
..options.baseUrl = 'https://www.domain.com/api'; | |
..options.connectTimeout = 5000; // 5 seconds | |
..options.receiveTimeout = 3000; // 3 seconds | |
try { | |
final response = await dio.post('/authenticate', data: {'login': 'user_name', 'password': 'password'}); | |
// Check response then response.data and convert response to your model | |
} on Dio.DioError catch (error) { | |
// Handle Dio exceptions here | |
} catch (error) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Instantiate a service and keep it in your DI container: | |
final service = NetworkService(baseUrl: 'https://www.domain.com/api'); | |
// Prepare a request: | |
final request = NetworkRequest( | |
type: NetworkRequestType.POST, | |
path: '/authenticate', | |
data: NetworkRequestBody.json({ | |
'login': 'user_name', | |
'password': 'password' | |
}), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AccessTokenResponse { | |
String? accessToken; | |
AccessTokenResponse.fromJson(Map<String, dynamic> json) { | |
accessToken = json['access_token']; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:freezed_annotation/freezed_annotation.dart'; | |
part 'NetworkRequestBody.freezed.dart'; | |
@freezed | |
class NetworkRequestBody with _$NetworkRequestBody { | |
const factory NetworkRequestBody.empty() = Empty; | |
const factory NetworkRequestBody.json(Map<String, dynamic> data) = Json; | |
const factory NetworkRequestBody.text(String data) = Text; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum NetworkRequestType { GET, POST, PUT, PATCH, DELETE } | |
class NetworkRequest { | |
const NetworkRequest({ | |
required this.type, | |
required this.path, | |
required this.data, | |
this.queryParams, | |
this.headers, | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:freezed_annotation/freezed_annotation.dart'; | |
part 'NetworkResponse.freezed.dart'; | |
@freezed | |
class NetworkResponse<Model> with _$NetworkResponse { | |
const factory NetworkResponse.ok(Model data) = Ok; | |
const factory NetworkResponse.invalidParameters(String message) = InvalidParameters; | |
const factory NetworkResponse.noAuth(String message) = NoAuth; //401 | |
const factory NetworkResponse.noAccess(String message) = NoAccess; //403 | |
const factory NetworkResponse.badRequest(String message) = BadRequest; //400 | |
const factory NetworkResponse.notFound(String message) = NotFound; //404 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NetworkService { | |
NetworkService({ | |
required this.baseUrl, // Base service url | |
dioClient, // Prepared Dio instance could be injected | |
httpHeaders, // Global headers could be provided as well | |
}) : this._dio = dioClient, | |
this._headers = httpHeaders ?? {}; | |
Dio? _dio; | |
final String baseUrl; | |
Map<String, String> _headers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dynamic body = request.data.whenOrNull( | |
json: (data) => data, | |
text: (data) => data, | |
); |
OlderNewer