'Dart: inferring non-nullable type in generics

I have two generic classes, and StreamProcessor needs to accept a nullable type as a generic parameter, while also maintaining a parametrized property which isn't nullable:

/// [T] must not be nullable (the data can not be null).
class DataProcessor<T extends Object> { /* ... */ }

/// [T] can be nullable if stream sends a checkpoint without any data attached.
class StreamProcessor<T extends Object?> {
  final DataProcessor<T> dataStream;
  //                  ^ 
  // Error: 'T' doesn't conform to the bound 'Object' of the type parameter 'T'.

  /// These events may be nullable or non-nullable, depending on the
  /// type of object passed to the constructor.
  final Stream<T> bareEventsStream;

  StreamProcessor(Stream<T> events) :
    dataStream = DataProcessor(events.where((e) => e != null)) { /* ... */ }
}

The compiler is rightfully unhappy about this situation (see the "Error" comment). I can make it happier by restricting the StreamProcessor type argument to a non-nullable type (extends Object) and then declaring bareEventsStream as Stream<T?>. But in that case, if the user of StreamProcessor uses a non-nullable type (e.g.: StreamProcessor<int>), the bareEventsStream will be a Stream<int?>, forcing them to use ! (null check operator) all over the code without a good reason, which seems backwards to sound null safety.

Is there a syntax which would, similarly to how T? infers a nullable type, infer a non-nullable type (when possible) from a generic parameter?

P.S. This is somewhat related to dart-lang/language#143 but I need "an opposite" of what's being discussed there.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source