Skip to content

Instantly share code, notes, and snippets.

@Monarene
Last active January 20, 2022 10:13
Show Gist options
  • Select an option

  • Save Monarene/db7da952987f0558e5836ea034ad4aa0 to your computer and use it in GitHub Desktop.

Select an option

Save Monarene/db7da952987f0558e5836ea034ad4aa0 to your computer and use it in GitHub Desktop.
To show how code for a toast would look like
class AuthVerify {
bool success;
bool isEmailVerify;
bool requiresOnboarding;
AuthVerify(
{this.success = false,
this.isEmailVerify = false,
this.requiresOnboarding = false});
}
class AuthState {
AuthFormError? authFormError;
bool loading;
String? token;
bool valid;
AuthState(
{this.loading = false,
this.authFormError,
this.token,
this.valid = false});
}
Future<AuthVerify> signIn(UserFormData userFormData,
[CancelToken? cancelToken]) async {
Response? response;
try {
state = AuthState(loading: true);
final ApiService apiService = await _reader(apiServiceProvider.future);
response = await apiService.client.post(ApiRoutes.login,
data: userFormData.toJson(), cancelToken: cancelToken);
final data = SignInResponse.fromJson(response.data);
final token = data.authToken;
final requiresOnboarding = !data.isProfessional;
if (!requiresOnboarding) {
LocalStoreHelper.saveInfo(token);
}
apiService.setToken(token);
state = AuthState(loading: false, token: token);
return AuthVerify(
success: response.statusCode == 200,
requiresOnboarding: requiresOnboarding);
} on DioError catch (e) {
AuthFormError? formError = AuthUtil.handleDioError(e);
state = AuthState(authFormError: formError);
Toast.showCustomErrorNotification(formError!.nonFieldError);
return AuthVerify(
success: response?.statusCode == 200,
isEmailVerify: formError.isEmailVerified == 'False');
} catch (e) {
final AuthFormError error =
AuthFormError(nonFieldError: SYSTEM_ERROR_MESSAGE);
Toast.showCustomErrorNotification(error.nonFieldError);
state = AuthState(authFormError: error);
}
return AuthVerify(success: response?.statusCode == 200);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment