Last active
March 5, 2025 17:25
-
-
Save mmaitlen/ef1d73f4767c6cda758726fa34679e6b to your computer and use it in GitHub Desktop.
Null callback shortcut?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Stumbled upon this, the code at the bottom compiles, but there is no output, | |
when I was thinking it might output something like >> list 123, 456 | |
Was looking for a shorthand to | |
final cb = callback; | |
if (cb != null) { | |
cb(['123','456']); | |
} | |
I know I can use callback!(['123','456']) inside conditional, but I would have expected callback?(['123','456']) | |
would also compile, but it doesn't. | |
Any chance there is a way to make it work as I guessed? | |
import 'package:test/test.dart'; | |
typedef RegisteredClientListCallback = void Function(List<String> clientIds); | |
main() { | |
RegisteredClientListCallback? callback = (clientIds) => print('list $clientIds'); | |
test('description', () { | |
callback ?? | |
(cb) { | |
cb(['123', '456']); | |
}; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment