What does ~ mean in Dart?

for example If you intend for a list to contain only strings, you can declare it as List<String> (read that as “list of string”)


This are generic type parameters. It allows specializations of classes.

List is a list that can contain any value (if no type parameter is passed dynamic is used by default). List<int> is a list that only allows integer values andnull`.

You can add such Type parameters to your custom classes as well.
Usually single upper-case letters are used for type parameter names like T, U, K but they can be other names like TKey ...

class MyClass<T> {
  T value;
  MyClass(this.value);
}

main() {
  var mcInt = MyClass<int>(5);
  var mcString = MyClass<String>('foo');
  var mcStringError = MyClass<String>(5); // causes error because `5` is an invalid value when `T` is `String`
}

See also https://www.dartlang.org/guides/language/language-tour#generics

Tags:

Dart