type'(PlatformException)=&gt; void'不是'(Object)=&gt;类型的子类型FutureOr <dynamic> Flutter

时间:2018-04-12 09:41:00

标签: dart flutter

我在Flutter中使用事件通道将Beacon数据从Native SDK返回到Flutter。这很好,直到最近Flutter升级。现在,我收到以下错误。

type '(PlatformException) => void' is not a subtype of type '(Object) => FutureOr<dynamic>

使用以下堆栈跟踪:

#0      _registerErrorHandler (dart:async/async_error.dart:22:60)
#1      _BufferingStreamSubscription.onError (dart:async/stream_impl.dart:146:16)
#2      new _BufferingStreamSubscription (dart:async/stream_impl.dart:113:10)
#3      new _ControllerSubscription (dart:async/stream_controller.dart)
#4      new _BroadcastSubscription (dart:async/broadcast_stream_controller.dart)
#5      _BroadcastStreamController._subscribe (dart:async/broadcast_stream_controller.dart:212:46)
#6      _ControllerStream._createSubscription (dart:async/stream_controller.dart:817:19)
#7      _StreamImpl.listen (dart:async/stream_impl.dart:466:9)
#8      _MyHomePageState.initPlatformState.<anonymous closure>.<anonymous closure> (file:///Users/chaythanyanair/Documents/Qburst/Learn/flutter_poc/lib/main.dart:95:43)
#9      _RootZone.runUnary (dart:async/zone.dart:1381:54)
#10     _FutureListener.handleValue (dart:async/future_impl.dart:129:18)
#11     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:633:45)
#12     Future._propagateToListeners (dart:async/future_impl.dart:662:32)
#13     Future._complete (dart:async/future_impl.dart:467:7)
#14     _SyncCompleter.complete (dart:async/future_impl.dart:51:12)
#15     MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart)
<asynchronous suspension>
#16     _MyHomePageState.initPlatformState.<anonymous closure> (file:///Users/chaythanyanair/Documents/Qburst/Learn/flutter_poc/lib/main.dart:89:24)
<asynchronous suspension>
#17     _RootZone.runUnary (dart:async/zone.dart:1381:54)
#18     _FutureListener.handleValue (dart:async/future_impl.dart:129:18)
#19     Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:633:45)
#20     Future._propagateToListeners (dart:async/future_impl.dart:662:32)
#21     Future._complete (dart:async/future_impl.dart:467:7)
#22     _SyncCompleter.complete (dart:async/future_impl.dart:51:12)
#23     User_Profile.getUser (package:flutter_poc/Models/User.dart)
<asynchronous suspension>
#24     _MyHomePageState.initPlatformState (file:///Users/chaythanyanair/Documents/Qburst/Learn/flutter_poc/lib/main.dart:69:24)
<asynchronous suspension>
#25     _MyHomePageState.initState (file:///Users/chaythanyanair/Documents/Qburst/Learn/flutter_poc/lib/main.dart:52:5)
#26     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3734:58)
#27     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3600:5)
#28     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2890:14)
#29     Element.updateChild 

这是我的EventChannel实现:

static const platform = const MethodChannel('samples.flutter.io/initialiseRanging');
static const stream =
  const EventChannel('samples.flutter.io/ranging');
try {
        await platform.invokeMethod('initialiseRanging').then((result){
          print(result);
          setState(() {
            _currentValue = result;
          });

          stream.receiveBroadcastStream().listen(_onEvent, onError: _onError);
        });

  } on PlatformException catch (e) {
        print( "{e.message}");
  }

这是_onEvent_onError函数的实现方式。

void _onEvent(Object event) {
    setState(() {
      _currentValue = event.toString();
    });
    print(event);
}

void _onError(PlatformException error) {
    print(error);
}

有关为何可能发生这种情况的任何想法?

1 个答案:

答案 0 :(得分:3)

void _onError(PlatformException error) {

应该是

void _onError(Object error) {

即使您只对PlatformException感兴趣,或者即使这是迄今为止发生的唯一例外,也不意味着不能成为其他人。预期的处理函数需要匹配参数类型,而不仅仅是您期望的异常。