Created
April 18, 2023 10:32
-
-
Save anvuive456/4721f8c966c21cc7051026378a6b51c2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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