Filters a sequence so that only events of a given type pass
In order to capture the Type correctly, it needs to be wrapped in a TypeToken as the generic parameter.
Given the way Dart generics work, one cannot simply use the
is T /
checks and castings with this method alone. Therefore, the
TypeToken class was introduced to capture the type of class you'd
ofType to filter down to.
new Observable.fromIterable([1, "hi"]) .ofType(new TypeToken<String>) .listen(print); // prints "hi"
As a shortcut, you can use some pre-defined constants to write the above in the following way:
new Observable.fromIterable([1, "hi"]) .ofType(kString) .listen(print); // prints "hi"
If you'd like to create your own shortcuts like the example above, simply create a constant:
const TypeToken<Map<Int, String>> kMapIntString = const TypeToken<Map<Int, String>>();
Observable<S> ofType<S>(TypeToken<S> typeToken) => transform(OfTypeStreamTransformer<T, S>(typeToken));