-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Open
Labels
P4area-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.devexp-linterIssues with the analyzer's support for the linter packageIssues with the analyzer's support for the linter packagetype-enhancementA request for a change that isn't a bugA request for a change that isn't a bug
Description
Dart should have a lint to ensure that users who combine conditional access and casting that the casting type is nullable. An alternative solution would be to have the user switch to using the bang (!) operator instead to show intent.
BAD:
bool? getBool(List<List<Object>?> input) {
return input.firstOrNull?.single as bool; // Non-null type combined with conditional access, will throw a TypeError
}
void main() {
final objects = <List<Object>?>[null, ["bar"], [2]];
getBool(objects);
}GOOD:
bool? getBool(List<List<Object>?> input) {
return input.firstOrNull?.single as bool?; // Nullable type combined with conditional access, no TypeError
}
bool? getBool(List<List<Object>?> input) {
return input.firstOrNull!.single as bool; // Using a bang operator instead
}
void main() {
final objects = <List<Object>?>[null, ["bar"], [2]];
getBool(objects);
}FMorschel
Metadata
Metadata
Assignees
Labels
P4area-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.devexp-linterIssues with the analyzer's support for the linter packageIssues with the analyzer's support for the linter packagetype-enhancementA request for a change that isn't a bugA request for a change that isn't a bug