server/lib/client.dart
2025-05-27 11:35:34 +03:00

58 lines
1.5 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:dio/dio.dart';
import 'package:server/constants.dart';
import 'package:shared_preferences/shared_preferences.dart';
final api = Dio(options);
final options = BaseOptions(baseUrl: 'https://api.matule.ru/api/');
initConnectivityChecker() async {
api.interceptors.add(
InterceptorsWrapper(
onRequest: (options, handler) async {
final connectivityResult = await Connectivity().checkConnectivity();
if (connectivityResult.isEmpty ||
connectivityResult.contains(ConnectivityResult.none)) {
return handler.reject(
DioException(
requestOptions: options,
error: 'У вас нет интернета',
type: DioExceptionType.connectionError,
),
);
} else {
return handler.next(options);
}
},
),
);
}
getLocalStorageInstance() async {
final prefs = await SharedPreferences.getInstance();
return prefs;
}
getToken() async {
final prefs = await getLocalStorageInstance();
if (prefs.containsKey(token)) {
return prefs.get(token);
}
}
setToken(value) async {
final prefs = await getLocalStorageInstance();
prefs.setString(token, value);
}
setKey(key, value) async {
final prefs = await getLocalStorageInstance();
prefs.setString(key, value);
}
getKey(key) async {
final prefs = await getLocalStorageInstance();
prefs.get(key);
}