fetchUser method

void fetchUser({
  1. dynamic onSuccess(
    1. User user
    ) = _defaultOnUserFetched,
  2. dynamic onError(
    1. FetchUserError user
    ) = _defaultOnUserFetchError,
  3. FetchUserParams params = const FetchUserParams._(),
})

Fetches user data.

Calls onSuccess with User data after fetching. In case any error was encountered during fetching calls onError instead.

params are added for backwards compatibility and for now are always empty.

Usage example:

final vkid = await VKID.getInstance();
vkid.fetchUser(
.   onSuccess: (user) => print(user.firstName),
    onError: (error) => print(error),
);

Implementation

void fetchUser({
  Function(User user) onSuccess = _defaultOnUserFetched,
  Function(FetchUserError user) onError = _defaultOnUserFetchError,
  FetchUserParams params = const FetchUserParams._(),
}) async {
  try {
    final List<Object?> result =
        await _platform.invokeMethod("fetchUser", []);
    onSuccess(User(
      result[0] as String,
      result[1] as String,
      result[2] as String? ?? "",
      result[3] as String? ?? "",
      result[4] as String? ?? "",
    ));
  } on PlatformException catch (e) {
    onError(e.code == "expired"
        ? const FetchUserTokenExpiredError._()
        : FetchUserOtherError._(e.message ?? ""));
  } catch (e) {
    onError(const FetchUserOtherError._("unknown error"));
  }
}