I receive an error message from Flutter saying :
"The semaphore timeout period has expired".
However, it returns results normally in Chrome and Android. I have been searching for solutions for a long time but haven't been able to solve it.
Below are my system environment and code. Could you please help me resolve this issue? Thank you.
Flutter Doctor Result :
My Code :
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
getResponse() async {
final res = await http.get(
Uri.parse('https://api.openai.com/v1/models'),
headers: {
'Authorization':
'Bearer $openaiKey',
'Content-Type': 'application/json'
},
);
final resJson = jsonDecode(res.body);
// ignore: avoid_print
print(resJson);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'Test',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: getResponse,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
I have tried other APIs and they return normally, but only when requesting the OpenAI API do I encounter this error.
Error Info :
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: The semaphore timeout period has expired. #0 IOClient.send io_client.dart:94 #1 BaseClient._sendUnstreamed base_client.dart:93 #2 _withClient http.dart:166 #3 _MyHomePageState.getResponse main.dart:35 Lost connection to device. Exited (sigterm)