dart assert code example
Example 1: dart test expect assert fail
import 'package:test/test.dart';
void main() {
test('assert throws assert', () {
expect(() {
assert(false);
}, throwsA(isA<AssertionError>());
});
}
Example 2: dart spread
arr1 = [1, 2, 3];
arr2 = [...arr1, 4, 5, 6]
// arr -> [1, 2, 3, 4, 5, 6]
Example 3: dart ?? operator
int val1;
final int val2 = 20;
console.log(val1 ?? val2); //result => 20 because val1 is null
----------------------------------
final int val1 = 30;
final int val2 = 20;
console.log(val1 ?? val2); //result => 30 because val1 is not null
Example 4: dart function syntax
sum(x, y) {
return x + y;
}
// This is where the app starts executing.
void main() {
print(sum(3,5)); // => 8
}