dio icon indicating copy to clipboard operation
dio copied to clipboard

Fix for "Future already completed" error in web

Open abhimanyupandian opened this issue 2 years ago • 5 comments

New Pull Request Checklist

  • [x] I have read the Documentation
  • [x] I have searched for a similar pull request in the project and found none
  • [x] I have updated this branch with the latest develop to avoid conflicts (via merge from master or rebase)
  • [x] I have added the required tests to prove the fix/feature I am adding
  • [x] I have updated the documentation (if necessary)
  • [x] I have run the tests and they pass

This merge request fixes / refers to the following issues: https://github.com/flutterchina/dio/issues/1497

Pull Request Description

The completer is called without checking if it is already complete, so a "Bad State" completer already completed error is thrown.

abhimanyupandian avatar Jun 08 '22 17:06 abhimanyupandian

@wendux could you please approve this PR so that workflows can run?

abhimanyupandian avatar Jun 12 '22 16:06 abhimanyupandian

1、环境:Web 浏览器:Chrom 102.0.5005.63(正式版本) (64 位) Flutter SDK::flutter_windows_3.0.2-stable.zip Android Studio:2021

2、现象: 1)BaseOptions有connectTimeout参数,执行Get时,从Api后台返回数据成功,然后报错,错误信息如下

errors.dart:251 Uncaught (in promise) Error: Bad state: Future already completed at Object.throw_ [as throw] (errors.dart:251:49) at _AsyncCompleter.new.completeError (future_impl.dart:15:79) at browser_adapter.dart:70:22 at _RootZone.runUnary (zone.dart:1685:54) at _FutureListener.then.handleValue (future_impl.dart:147:18) at handleValueCallback (future_impl.dart:766:44) at _Future._propagateToListeners (future_impl.dart:795:13) at [_complete] (future_impl.dart:557:7) at future.dart:420:15 at internalCallback (isolate_helper.dart:48:19)

2)BaseOptions没有connectTimeout参数时,返回数据成功,没有报错。

3、代码: ` import 'package:dio/dio.dart';

class _DioTestState extends State<DioTest> { @override Widget build(BuildContext context) { return Wrap( children: <Widget>[ ElevatedButton( onPressed: () { getData() ;}, child: const Text('test get'), ) ], ); }

void getData() { BaseOptions options = BaseOptions( baseUrl: 'http://127.0.0.1:9001', connectTimeout: 5000, receiveTimeout: 3000, ); Dio dio = Dio(options);

dio.get('/openApi/resTest/no_need_token').then((res) {
  print(res.toString());
}).catchError((e) {
  print(e.toString());
});

} } `

bjzhangyongbin avatar Jun 17 '22 01:06 bjzhangyongbin

@wendux When will this PR be merged? I've been getting errors all the time and I'm in great trouble. I would be very happy if you could support me!

manabu0926 avatar Jul 26 '22 15:07 manabu0926

Unsure that this fix is enough, I've tried it and keep getting (less) errors

HugoHeneault avatar Aug 18 '22 14:08 HugoHeneault

Fixed it for me. Would be good to see this PR pushed as these web bugs are annoying (even if they don't stop anything)

gslender avatar Aug 21 '22 01:08 gslender