TelegramInitData.fromRawString constructor

TelegramInitData.fromRawString(
  1. String data
)

Implementation

factory TelegramInitData.fromRawString(String data) {
  Map<String, String?> validationData =
      DataParser.parseAmpersandSeparatedData(data);

  // Parse user URI json string into Dart Map
  Map<String, dynamic> userData =
      jsonDecode(Uri.decodeFull(validationData['user']!));

  TelegramUser user = TelegramUser(
    isPremium: userData['is_premium'],
    id: userData['id'],
    budledData: jsonEncode(userData),
    firstname: userData['first_name'],
    lastname: ((userData['last_name'] as String?)?.isNotEmpty ?? false)
        ? userData['last_name']
        : null,
    username: userData['username'],
    languageCode: userData['language_code'],
    allowsWriteToPm: userData['allows_write_to_pm'],
  );

  int? chatInstance = validationData['chat_instance']?.isNotEmpty ?? false
      ? int.parse(validationData['chat_instance']!)
      : null;
  String? chatType = validationData['chat_type'];
  String? queryId = validationData['query_id'];

  int authDate = int.parse(validationData['auth_date']!);
  String hash = validationData['hash']!;

  return TelegramInitData(
    user: user,
    chatInstance: chatInstance,
    queryId: queryId,
    chatType: chatType,
    authDate: authDate,
    hash: hash,
    raw: data,
  );
}