0
votes

I've enabled strict mode in my Dart code-base today and have fixed most of the warnings and errors. This one I'm not sure how to fix ...

This is my method signature:

StreamSubscription listen(Type eventType, void onData(Object event), { Function onError, void onDone(), bool cancelOnError}) {

   ... code here ...

}

When doing: _bus.listen(OnRowDoubleClick, (OnRowDoubleClick event){ I'm getting

The argument type '(OnRowDoubleClick) -> Null' can't be assigned to the parameter type `(Object) -> void`

I can change the method signature to

StreamSubscription listen(Type eventType, Null onData(Object event), {...`, 

(which doesn't make sense since I don't actually want to return anything), the error changes to

The argument type '(OnRowDoubleClick) -> Null' can't be assigned to the parameter type `(Object) -> Null`

Changing the method call to this:

_bus.listen(OnRowDoubleClick, (event){

The error goes away, but now I have to cast event as OnRowDoubleClick the whole time.

Is there a better way of dong this?

* Solution *

Solution is to mark the listener with T:

StreamSubscription listen<T>(Type eventType, Null onData(T event), {

Now you can call _bus.listen<OnRowDoubleClick>(OnRowDoubleClick, (event){ and the event will be of type OnRowDoubleClick

1

1 Answers

2
votes

The problem is that listen expects the argument onData to be a function that takes any Dart Object as an argument. You are passing a function that only takes OnRowDoubleClick as an argument, so strong mode gives an error.

You could change your listen method so that it takes a type parameter, take a look at Stream#listen for an example.