This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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, |
NewerOlder