ofType<S> method

  1. @Deprecated('Please use whereType instead')
Observable<S> ofType<S>(
  1. TypeToken<S> typeToken
@Deprecated('Please use whereType instead')

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 / as T checks and castings with this method alone. Therefore, the TypeToken class was introduced to capture the type of class you'd like 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"])
  .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>>();


@Deprecated('Please use whereType instead')
Observable<S> ofType<S>(TypeToken<S> typeToken) =>
    transform(OfTypeStreamTransformer<T, S>(typeToken));