前端用的是flutter,使用dio的get请求,加了connecttimeout后出现Bad state: Future already completed,但是能成功拿到数据,有大佬知道是什么问题吗?
前端
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
loadData();
}
void loadData() async {
try {
Dio dio = Dio();
dio.options.connectTimeout = 5000;
dio.options.receiveTimeout = 5000;
dio.get('http://127.0.0.1:5000').then((value) => {print(value.data)});
} catch (e) {
print(Fluttertoast.showToast(msg: e.toString()));
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
routes: {
"/": (context) => const HomePage(),
},
);
}
}
服务器端
app = Flask(__name__)
CORS(app)
@app.route('/')
def demo():
return '123'
123
Error: Bad state: Future already completed
at Object.throw_ [as throw] (http://localhost:49846/dart_sdk.js:5080:11)
at _AsyncCompleter.new.completeError (http://localhost:49846/dart_sdk.js:35274:51)
at http://localhost:49846/packages/dio/src/interceptors/log.dart.lib.js:651:25
at _RootZone.runUnary (http://localhost:49846/dart_sdk.js:40511:59)
at _FutureListener.then.handleValue (http://localhost:49846/dart_sdk.js:35438:29)
at handleValueCallback (http://localhost:49846/dart_sdk.js:35999:49)
at Function._propagateToListeners (http://localhost:49846/dart_sdk.js:36037:17)
at _Future.new.[_complete] (http://localhost:49846/dart_sdk.js:35864:25)
at http://localhost:49846/dart_sdk.js:34994:30
at internalCallback (http://localhost:49846/dart_sdk.js:26685:11)
遇到相同的问题,返回两次异常error。处理了一次,超时这次就没法捕获了。
同问
dio请求出现Bad state: Future already completed 有两种可能性。1、dio的connectTime BUG,更新版本试试4.0.4和4.0.6或者把connectTimeout 注释掉。2、如果cors的原因。配置一下跨域请求。
https://github.com/flutter/flutter/issues/74407
https://github.com/flutter/flutter/issues/71006
也有可能是 杀毒软件的问题
dio.get('http://127.0.0.1:5000').then((value) => {print(value.data)});
这一句换个写法试试
var value = await dio.get('http://127.0.0.1:5000');
print(value.data);