DropboxToken.fromJson constructor

DropboxToken.fromJson(
  1. Map<String, dynamic> json
)

Creates a DropboxToken from a JSON map.

Implementation

factory DropboxToken.fromJson(Map<String, dynamic> json) {
  final dynamic expiresInValue = json['expires_in'];
  DateTime expires;
  // Handles both integer (seconds) and ISO 8601 string formats for expiration.
  if (expiresInValue is int) {
    expires = DateTime.now().add(Duration(seconds: expiresInValue));
  } else if (expiresInValue is String) {
    expires = DateTime.parse(expiresInValue);
  } else {
    throw Exception("Invalid 'expires_in' format");
  }
  return DropboxToken(
    accessToken: json['access_token'] as String,
    refreshToken: json['refresh_token'] as String?,
    tokenType: json['token_type'] as String,
    expiresIn: expires,
  );
}