#flutter #dart #stream #try-catch #connectivity
#трепетание #дротик #поток #попробуй-поймай #возможность подключения
Вопрос:
Я не могу перехватить исключения, которые генерируются внутри connectivity.onConnectivityChanged.listen
функции. Я пытался использовать блок try-catch и onError
функцию, но я не могу заставить их работать. Кто-нибудь знает, что я делаю не так?
import 'package:flutter/foundation.dart';
import 'dart:async';
import 'package:connectivity/connectivity.dart';
import 'package:dio/dio.dart';
class DioConnectivityRequestRetrier {
final Dio dio;
final Connectivity connectivity;
DioConnectivityRequestRetrier({
@required this.dio,
@required this.connectivity,
});
Future<Response> scheduleRequestRetry(RequestOptions requestOptions) async {
StreamSubscription streamSubscription;
final responseCompleter = Completer<Response>();
try {
connectivity.onConnectivityChanged.listen(
(connectivityResult) {
if (connectivityResult != ConnectivityResult.none) {
// streamSubscription.cancel();
// An exception when dio.request fails
throw Exception;
responseCompleter.complete(
dio.request(
requestOptions.path,
cancelToken: requestOptions.cancelToken,
data: requestOptions.data,
onReceiveProgress: requestOptions.onReceiveProgress,
onSendProgress: requestOptions.onSendProgress,
queryParameters: requestOptions.queryParameters,
options: requestOptions,
),
);
}
},
).onError((e) {
throw e;
});
} catch (e) {
rethrow;
}
return responseCompleter.future;
}
}
Вывод из консоли отладки при возникновении исключения внутри connectivity.onConnectivityChanged.listen
:
E/flutter ( 8336): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: Exception
E/flutter ( 8336): #0 DioConnectivityRequestRetrier.scheduleRequestRetry.<anonymous closure>
package:eindproduct/…/interceptor/dio_connectivity_request_retrier.dart:26
E/flutter ( 8336): #1 _rootRunUnary (dart:async/zone.dart:1198:47)
E/flutter ( 8336): #2 _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter ( 8336): #3 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
E/flutter ( 8336): #4 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:357:11)
E/flutter ( 8336): #5 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:285:7)
E/flutter ( 8336): #6 _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:127:11)
E/flutter ( 8336): #7 _MapStream._handleData (dart:async/stream_pipe.dart:224:10)
E/flutter ( 8336): #8 _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:157:13)
E/flutter ( 8336): #9 _rootRunUnary (dart:async/zone.dart:1198:47)
E/flutter ( 8336): #10 _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter ( 8336): #11 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
E/flutter ( 8336): #12 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:357:11)
E/flutter ( 8336): #13 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:285:7)
E/flutter ( 8336): #14 _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:127:11)
E/flutter ( 8336): #15 _MapStream._handleData (dart:async/stream_pipe.dart:224:10)
E/flutter ( 8336): #16 _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:157:13)
E/flutter ( 8336): #17 _rootRunUnary (dart:async/zone.dart:1198:47)
E/flutter ( 8336): #18 _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter ( 8336): #19 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
E/flutter ( 8336): #20 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:357:11)
E/flutter ( 8336): #21 _DelayedData.perform (dart:async/stream_impl.dart:611:14)
E/flutter ( 8336): #22 _StreamImplEvents.handleNext (dart:async/stream_impl.dart:730:11)
E/flutter ( 8336): #23 _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:687:7)
E/flutter ( 8336): #24 _rootRun (dart:async/zone.dart:1182:47)
E/flutter ( 8336): #25 _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter ( 8336): #26 _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter ( 8336): #27 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
E/flutter ( 8336): #28 _rootRun (dart:async/zone.dart:1190:13)
E/flutter ( 8336): #29 _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter ( 8336): #30 _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter ( 8336): #31 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
E/flutter ( 8336): #32 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter ( 8336): #33 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
Ответ №1:
Попробуйте что-то вроде этого:
try {
/// YOUR CODE HERE ///
} on PlatformException catch (e) {
print(e.toString());
}
Комментарии:
1. Вы можете видеть в моем примере кода, что я уже пробовал использовать блок try-catch, но это не работает. Добавление
on Exception
также не имеет значения.