getToken method

Future<String> getToken()

Define a function to get a token from the API

Implementation

Future<String> getToken() async {
  // Set the base URL for the API
  String baseUrl = _baseurl!;
  String email = _email!;
  String apikey = _apikey!;

  // Set the endpoint for the API call
  String endpoint = '/Account/GetToken';

  // Set the headers for the HTTP request
  Map<String, String> headers = {
    'Content-Type': 'application/json',
    'Cache-Control': 'no-cache',
  };

  // Set up the HTTP client
  http.Client client = http.Client();

  // Make the HTTP POST request to the API
  http.Response response = await client.post(
    Uri.parse(baseUrl + endpoint),
    headers: headers,
    body: json.encode({'email': email, 'apikey': apikey}),
  );

  // Check the status code of the response
  if (response.statusCode == 200) {
    // If the request was successful, parse the JSON response
    String data = json.decode(json.encode(response.body));
    // Return the token
    return data;
  } else {
    // If the request was not successful, throw an exception
    throw Exception('Failed to get token');
  }
}