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
427 stars 66 forks source link

late keyword is not working when defining a specific type #412

Closed phamnhuvu-dev closed 1 year ago

phamnhuvu-dev commented 1 year ago

I am not sure if it is a bug or not, I could write these codes by hand late Machine _machine; without any problems, so I want to ask about this.

Info: Version: code_builder: ^4.4.0 Dart SDK version: 2.18.2 (stable) (Tue Sep 27 13:24:11 2022 +0200) on "macos_arm64"

Code:

Field(
  (updates) => updates
    ..name = '_machine'
    ..late = true
    ..type = refer('Machine'),
)

Current behavior: Generate:

Machine _machine;

Expect behavior Generate:

late Machine _machine;
Snehal-Singh174 commented 1 year ago

The latest version will add late keyword only when create a late field using null-safety. Hence, you have to specify useNullSafetySyntax: true inside DartEmitter

DartEmitter(useNullSafetySyntax: true)

Refer the test of field: https://github.com/dart-lang/code_builder/blob/master/test/specs/field_test.dart

phamnhuvu-dev commented 1 year ago

@Snehal-Singh174 Thank you, I got it