getGamesServicesId method

String? getGamesServicesId()

Retrieves the Game Services ID for the current user (Play Games ID on Android or Game Center ID on iOS).

This method searches the linked provider data for the Game Services provider (either Play Games or Game Center) and returns the corresponding user ID (uid).

Returns the Game Services ID if found, or null if the user is not linked with either Play Games or Game Center.

Throws UnimplementedError for unsupported platforms (non-Android, non-iOS).

Implementation

String? getGamesServicesId() {
  try {
    // Return Play Games ID on Android.
    if (Platform.isAndroid) {
      return providerData
          .firstWhere(
            (UserInfo info) =>
                info.providerId == PlayGamesAuthProvider.PROVIDER_ID,
          )
          .uid;
    }

    // Return Game Center ID on iOS.
    if (Platform.isIOS) {
      return providerData
          .firstWhere(
            (UserInfo info) =>
                info.providerId == GameCenterAuthProvider.PROVIDER_ID,
          )
          .uid;
    }

    throw UnimplementedError('This platform is not supported.');
  } catch (e) {
    // Return null if no provider is found or if an error occurs.
    return null;
  }
}