voidmain() {
String? a ='a';
String? b =null;
a ??='c'; // 'c' will not be assigned to a because a is not null.
b ??='c'; // 'c' will be assigned to b because b is null.print(a); // aprint(b); // c
}
// a ia a required unnamed parameter.// b is an optional unnamed (aka positional) parameter whose default value is null.// c is an optional unnamed (aka positional) parameter with a default value.classMyClass {
MyClass(this.a, [this.b, this.c ='default!']) {
print('a = $a, b = $b, c = $c');
}
String a;
String? b;
String c;
}
voidmyFunction(
String a, [
String? b,
String c ='default!',
]) {
print('a = $a, b = $b, c = $c');
}
voidmain() {
MyClass('a');
MyClass('a', 'b');
MyClass('a', 'b', 'c');
myFunction('a');
myFunction('a', 'b');
myFunction('a', 'b', 'c');
}
Example of named parameters
// a ia a required unnamed parameter.// b ia a required named parameter.// c is an optional named parameter whose default value is null.// d is an optional named parameter with a default value.classMyClass {
MyClass(this.a, {requiredthis.b, this.c, this.d ='default!'}) {
print('a = $a, b = $b, c = $c, d = $d');
}
String a;
String b;
String? c;
String d;
}
voidmyFunction(
String a, {
requiredString b,
String? c,
String d ='default!',
}) {
print('a = $a, b = $b, c = $c, d = $d');
}
voidmain() {
MyClass('a', b:'b');
MyClass('a', b:'b', c:'c');
MyClass('a', b:'b', c:'c', d:'d');
myFunction('a', b:'b');
myFunction('a', b:'b', c:'c');
myFunction('a', b:'b', c:'c', d:'d');
}
How to convert List<dynamic> to List<String>
final xs =<dynamic>['a', 'b', 'c'];
print(xs.runtimeType); // List<dynamic>final ys =List<String>.from(xs);
print(ys.runtimeType); // List<String>// DON’T use cast() when a nearby operation will do// https://dart.dev/effective-dart/usage#dont-use-cast-when-a-nearby-operation-will-do// AVOID using cast()// https://dart.dev/effective-dart/usage#avoid-using-castfinal zs = xs.cast<String>();
print(zs.runtimeType); // CastList<dynamic, String>
About
Personal playground for experimenting with new ideas in Dart