minor login fixes
This commit is contained in:
@@ -18,13 +18,29 @@ class LoginController extends ChangeNotifier {
|
||||
final model = LoginScreenModel();
|
||||
late SharedPreferences prefs;
|
||||
BuildContext context;
|
||||
bool _disposed = false;
|
||||
|
||||
LoginController(this.context);
|
||||
|
||||
Future<ApiResponse<LoginScreenModel>> login(String username, String password) async {
|
||||
@override
|
||||
void dispose() {
|
||||
_disposed = true;
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _safeNotifyListeners() {
|
||||
if (!_disposed) {
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<ApiResponse<LoginScreenModel>> login(
|
||||
String username,
|
||||
String password,
|
||||
) async {
|
||||
try {
|
||||
model.isLoading = true;
|
||||
notifyListeners();
|
||||
_safeNotifyListeners();
|
||||
|
||||
prefs = await SharedPreferences.getInstance();
|
||||
|
||||
@@ -35,7 +51,7 @@ class LoginController extends ChangeNotifier {
|
||||
logger.i(response.toString());
|
||||
|
||||
model.isLoading = false;
|
||||
notifyListeners();
|
||||
_safeNotifyListeners();
|
||||
|
||||
if (response.containsKey('token')) {
|
||||
model.status = 'SUCCESS';
|
||||
@@ -47,26 +63,32 @@ class LoginController extends ChangeNotifier {
|
||||
prefs.setString('lastName', response['lastName']);
|
||||
prefs.setString('phone', response['phone']);
|
||||
prefs.setString('userId', response['uuid']);
|
||||
prefs.setString('initials', response['firstName'].substring(0, 1) + response['lastName'].substring(0, 1));
|
||||
prefs.setString(
|
||||
'initials',
|
||||
response['firstName'].substring(0, 1) +
|
||||
response['lastName'].substring(0, 1),
|
||||
);
|
||||
|
||||
return ApiResponse.success(model);
|
||||
} else {
|
||||
model.errorMessage =
|
||||
response['errorMessage'] ??
|
||||
"Problem with the transaction. Please try again or contact support";
|
||||
"Problem with the operation. Please try again or contact support";
|
||||
return ApiResponse.failure(model.errorMessage!);
|
||||
}
|
||||
} on DioException catch (e, s) {
|
||||
logger.e(s);
|
||||
model.isLoading = false;
|
||||
notifyListeners();
|
||||
return ApiResponse.failure(e.response?.data['errors'][0] ?? "Unknown error");
|
||||
_safeNotifyListeners();
|
||||
return ApiResponse.failure(
|
||||
e.response?.data['errors'][0] ?? "Unknown error",
|
||||
);
|
||||
} catch (e, s) {
|
||||
logger.e(s);
|
||||
model.isLoading = false;
|
||||
notifyListeners();
|
||||
_safeNotifyListeners();
|
||||
return ApiResponse.failure(
|
||||
"Problem processing that transaction. Please try again or contact support",
|
||||
"Problem processing that operation. Please try again or contact support",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user