Reputation: 1023
Doesn't seem like skipWhile is doing anything... or am I understanding this wrong?
Below code... I am expecting skipWhile to remove two entries!
Dart version (from flutter doctor): Dart version 2.9.0 (build 2.9.0-10.0.dev 7706afbcf5)
import 'package:flutter_test/flutter_test.dart';
class Car {
final String name;
final bool active;
final int wheels;
Car({this.name, this.active=true, this.wheels=4});
}
void main() {
test("Check skipWhile", () {
List dataSet = [
Car(name: "Thunder", active: false),
Car(name: "Lightening", active: false),
Car(name: "Dinky", wheels: 3),
Car(name: "Camry"),
Car(name: "Outback"),
];
List activeCars = dataSet.skipWhile((car) => car.active).toList();
expect(activeCars.length, 3);
});
}
Upvotes: 0
Views: 809
Reputation: 31219
If you read the documentation for skipWhile
you will see:
Returns an Iterable that skips leading elements while test is satisfied.
So it just skips the leading elements and not all elements.
What you properly want instead is where
which does:
Returns a new lazy Iterable with all elements that satisfy the predicate test.
So if I change your skipWhile
to where
in your example, I will now get the length of 3:
class Car {
final String name;
final bool active;
final int wheels;
Car({this.name, this.active = true, this.wheels = 4});
}
void main() {
final dataSet = [
Car(name: "Thunder", active: false),
Car(name: "Lightening", active: false),
Car(name: "Dinky", wheels: 3),
Car(name: "Camry"),
Car(name: "Outback"),
];
final activeCars = dataSet.where((car) => car.active).toList();
print(activeCars.length); // 3
}
Upvotes: 2