Skip to content
pubscout

analyzer_buffer

0.3.4, released 3 months ago by dash-overflow.net

Helper for code generators to write code with import prefixes and default values

$ flutter pub add analyzer_buffer
Source repository

What it does

AnalyzerBuffer provides a buffer that understands import prefixes, default values, and type writing for code generators. It offers a template‑based syntax to reference types and constants from other files and automatically manages imports.

AI-written summary of version 0.3.4, based on the package's README and metadata. Report an inaccuracy

Use it when

  • Writing source_gen generators that need to reference types or constants from other packages
  • Generating code that must preserve import prefixes or copy default values
  • Creating boilerplate code that requires type or constant conversion to source
  • Automating import management in generated files

Skip it when

  • Runtime code that does not involve code generation
  • Scenarios where manual string concatenation is simpler than a buffer
  • Projects that do not use the analyzer or source_gen

Strengths

  • Supports import prefixes and automatic import insertion
  • Copies default values and constants preserving prefixes
  • Provides writeType and DartType.toCode for type conversion
  • Custom string interpolation for dynamic content

Weaknesses

  • Only useful for code generation, not runtime
  • Depends on analyzer and source_gen, so not suitable for pure Dart projects
  • Requires understanding of analyzer APIs

Setup

Kind
Pure Dart
Effort
Low
Code generation
Yes (build_runner)
Direct dependencies
5

Add analyzer_buffer to pubspec, import it in your generator, and use AnalyzerBuffer.part(library.element) to start writing.

```dart
final buffer = AnalyzerBuffer.part(library.element);
buffer.write('''
class Example {
  final #{{dart:async|StreamController}} controller;
  final #{{riverpod|Provider}} provider;
}
''');
return buffer.toString();
```

Alternatives

Packages that do the same job and could replace analyzer_buffer.

  • build

    Package for authoring build_runner compatible code generators.

  • flutter_gen_runner

    Generates strongly-typed Dart classes for Flutter assets, fonts, and colors to avoid string-based APIs.

  • flutter_gen

    A code generator for your assets, fonts, and colors to eliminate string-based APIs.

  • source_helper

    Utilities to help with Dart source code generation and string literal formatting.

Works with

Packages commonly used together with analyzer_buffer: ones it builds on, ones built on it, and its own family.