login method Null safety

Future<AnnouncementLoginData?> login(
  1. {required String? username,
  2. required String? password,
  3. GeneralCallback<AnnouncementLoginData>? callback}
)

Implementation

Future<AnnouncementLoginData?> login({
  required String? username,
  required String? password,
  GeneralCallback<AnnouncementLoginData>? callback,
}) async {
  try {
    final Response<Map<String, dynamic>> response = await dio.post(
      '/login',
      data: <String, String?>{
        'username': username,
        'password': password,
        'fcmToken': fcmToken,
      },
    );
    final AnnouncementLoginData loginData =
        AnnouncementLoginData.fromJson(response.data!);
    setAuthorization(loginData.key);
    this.username = username;
    this.password = password;
    loginType = AnnouncementLoginType.normal;
    if (callback != null) {
      callback.onSuccess(loginData);
    }
    return loginData;
  } on DioError catch (dioError) {
    if (callback == null) {
      rethrow;
    } else {
      if (dioError.isUnauthorized) {
        callback.onError(
          GeneralResponse(
            statusCode: 401,
            message: ApLocalizations.current.loginFail,
          ),
        );
      } else {
        callback.onFailure(dioError);
      }
    }
  }
  return null;
}