Skip to content

Instantly share code, notes, and snippets.

@anvuive456
Created April 18, 2023 10:32
Show Gist options
  • Select an option

  • Save anvuive456/4721f8c966c21cc7051026378a6b51c2 to your computer and use it in GitHub Desktop.

Select an option

Save anvuive456/4721f8c966c21cc7051026378a6b51c2 to your computer and use it in GitHub Desktop.
import 'package:http/http.dart' as http;
Future<String> getIdToken({bool? force}) async {
// fetch update token
return "abc*******";
}
Future<void> main() async {
final client = http.Client();
String token = await getIdToken();
try {
final response = await client.get(Uri.parse("https://api.***.server"),
headers: {'Authentication': "Bearer $token", "Access-Control-Allow-Origin": "*"});
print(response);
if(response.statusCode == 401){
//retry get id token
token = await getIdToken();
final response = await client.get(Uri.parse("https://api.***.server"),
headers: {'Authentication': "Bearer $token", "Access-Control-Allow-Origin": "*"});
print(response);
}
} on http.ClientException catch ( e) {
return Future.error(e.message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment