dart-lang / code_builder

A fluent API for generating valid Dart source code
https://pub.dev/packages/code_builder
BSD 3-Clause "New" or "Revised" License
423 stars 66 forks source link

Unclear how to generate an anonymous function #381

Closed eseidel closed 1 year ago

eseidel commented 1 year ago

It's not clear (from examples or unit tests) how one might generate an anonymous function for passing to a call, e.g.:

setState(() {
  doSomething();
})

Sure I could use a Code() block, but I'm looking for the Method() variant. e.g.

refer('setState').call([
// What do I put here?
]);

FunctionType is allowed there (its an expression), where as Method is not, but I suspect I actually want a Method, maybe just one without a name? And then wrap it in a CodeExpression?

eseidel commented 1 year ago

Found it. Method().closure it the magic.

https://github.com/dart-lang/code_builder/blob/master/test/specs/method_test.dart presumably needs a test, maybe I'll add one.