Code Generation with code_builder
When working with the code_builder package:
Critical Rules — Never Violate
- NEVER use
Code.scope. - ALWAYS use
referwith package URL for ALL types — evendart:coretypes. - NEVER mix
referand string interpolation — they are incompatible. - Use
CodewithBlock.of— never useStringBuilder. - NEVER use
DartEmitterinside generator methods — only the maingeneratemethod is allowed to use it.
Examples
// CORRECT - Build code in separate Code objects
statements
..add(Code('if (condition != '))
..add(refer('String', 'dart:core').code)
..add(Code(') {'))
// CORRECT - Use refer with package URL
refer('EncodingShape.simple', 'package:tonik_util/tonik_util.dart')
// WRONG - Don't use Code.scope
Code.scope((allocate) => '...')
// WRONG - Don't mix refer with string interpolation
Code('if (condition != ${refer('String', 'dart:core').code})')
// WRONG - Don't use refer without package URL
refer('String') // Missing 'dart:core'
// WRONG - Don't use DartEmitter in generator methods
Code('${nullableType.accept(emitter)} $variableName;') // emitter not allowed
// CORRECT - Build code in separate Code objects
Block.of([
nullableType.code,
Code(' $variableName;'),
])
Converted and distributed by TomeVault — claim your Tome and manage your conversions.