Open
Description
I tried to make the following code with switch expression.
static FirebaseOptions forPlatform({FirebaseOptions? android, FirebaseOptions? ios}) {
void throwUnsupportedPlatform(TargetPlatform platform) => throw UnsupportedError('DefaultFirebaseOptions are not supported for $platform.');
return switch (defaultTargetPlatform) {
TargetPlatform.android => android ?? throwUnsupportedPlatform(defaultTargetPlatform),
TargetPlatform.iOS => ios ?? throwUnsupportedPlatform(defaultTargetPlatform),
_ => throwUnsupportedPlatform(defaultTargetPlatform),
};
}
The analyzer complains that A value of type 'void' can't be returned from the method 'forPlatform' because it has a return type of 'FirebaseOptions'.
. I imagined the analyzer would be able to know that throwUnsupportedPlatform
always throws, so this is valid code.
This code works:
return switch (defaultTargetPlatform) {
TargetPlatform.android => android ?? (throw UnsupportedError('DefaultFirebaseOptions are not supported for $defaultTargetPlatform.')),
TargetPlatform.iOS => ios ?? (throw UnsupportedError('DefaultFirebaseOptions are not supported for $defaultTargetPlatform.')),
_ => throw UnsupportedError('DefaultFirebaseOptions are not supported for $defaultTargetPlatform.'),
};