getEmail method

Future<String?> getEmail()

Get the email from the access token. Github email scope is not required. If the user does not give permission to access the email, the email will not be returned.

Implementation

Future<String?> getEmail() async {
  if (!params!.scopes.contains("email")) {
    return null;
  }
  try {
    response = await http.get(
      Uri.parse(emailUrl),
      headers: {
        'Accept': 'application/json',
        'Authorization': "Bearer $accessToken",
      },
    );
    List emails = jsonDecode(response.body);
    if (emails.isEmpty) {
      return null;
    }

    var match = emails.where((element) => element['primary'] == true);

    if (match.isEmpty) {
      return null;
    }

    String? email = match.first['email'];
    return email;
  } catch (e) {
    message = "Status Code: ${response.statusCode}\n\n Response Body: ${response.body} \n\n Error: ${e.toString()}";
    return null;
  }
}