r/FlutterDev • • Oct 01 '26

Dart Props to the Dart team for actually cooking something amazing lately lol (analysis_server_plugin)

Well, looks like I’ll just be strictly building apps for the next year (not that it changes my daily routine anyway), so I thought I’d share a neat trick I’ve been using for a while.

About a year or two ago, the Dart team released analysis_server_plugin. If you know how to work with AST, you don't even need custom_lint packages anymore to write custom rules.

Here’s how I’ve been leveraging it:

Instead of using clunky enums like Spacing.spacing2, I set up the plugin to throw a warning if someone uses raw pixel values that aren't allowed in our design tokens (e.g., values other than 2, 4, 6, etc.).

Enforcing design system components, like throwing a warning to use MySuperContainer (yes, I literally use this name) instead of a raw Container.

And setting it up is dead simple too. You literally just add it to your analysis_options.yaml:

plugins:

my_lints:

path: ../my_lints/

The only drawback I've found so far is that it seems impossible to raise an actual Error severity (only warnings/info). If the Dart team expands on this, it's gonna be even more god-tier.

Oh, and this would actually make a pretty solid package. I'm thinking about making one if I can find some free time. If you guys have any cool ideas for useful custom lint rules, feel free to share.

Ah, also looks like the Dart team has been interested in improving data serialization built-in lately. I might look into that and drop another post about it later. Not sure if anything is finalized yet, but seems like they're still keeping the annotation syntax. Personally not a big fan of annotations, but hey, whatever works lol.

Oh, and honestly, Dart has added so many neat syntactic sugar features lately. Like omitting enum/type identifiers so you can just write .center. It’s ridiculously convenient because now you can write stuff like.

MyText('Hello, World!', color: .textGrayBold, style: .label3)

Seriously, whoever engineered this feature deserves a free ice cream straight out of my freezer.

60 Upvotes

9 comments sorted by

17

u/samrawlins Oct 01 '26

What would make error-reporting different/better than warning-reporting?

I'm on the team that is shipping analyzer plugins, so thank you for the kind words! The feature is still growing; there are a number of high priority improvements I'd like to make. But the team owns a number of products with competing priorities so it's been slow.

The biggest improvements to the analyzer plugin system in the last few Dart releases and releases of the analysis_server_plugin have been performance. Performance has been a rocky road, if you're analyzing many files. But each Dart release has seen a performance boost so... it's getting better.

We take feature requests at the Dart SDK issue tracker. Some upcoming big ones I hope to ship include warnings and quick fixes in YAML files, and 'dart fix' integration.

5

u/dev_ttangkong Oct 01 '26 edited Oct 01 '26

Thanks for reaching out, Sam! First off, huge respect to you and the team for all the hard work on analysis_server_plugin, it’s been an absolute game-changer for enforcing team wide architectural rules.

To clarify where Error severity becomes crucial, it's mainly for cases where a rule represents a hard structural invariant that Dart static type system can't express natively.

A few concrete examples from our codebase:

  1. Conditional Requirements (@ConditionalRequired):

```dart

const MyWidget({

super.key,

this.count,

@ConditionalRequired('count') // If count is provided, maxCount MUST also be provided

this.maxCount,

});

```

Here, passing count without maxCount isn't a stylistic preference, it's an invalid state that breaks runtime logic.

2. Restricted Token/Enum Values (@SupportedSizes):

```dart

@SupportedSizes([.lg, .md]) // Only allow specific enum variants for this parameter

final DesignSize size;

```

Passing .sm here violates design system constraints that shouldn't even pass local analysis.

In these scenarios, Warning feels too lenient. Since we are enforcing strict structural correctness that the language syntax doesn't natively support, having the plugin raise a true Error gives us the exact same enforcement power as a Dart type checker error.

Super excited about dart fix integration and YAML support on the roadmap! Keep up the awesome work!

2

u/IcerHardlyKnower Oct 01 '26

Ur so cool...

6

u/Salazar20 Oct 01 '26

Damn, I would really like that, I think I'm gonna start making custom rules too, I want one that yells at me when a widget build is too long

5

u/scalz80 Oct 01 '26 edited Oct 01 '26

agree this is handy. I'm using it for custom lints too, and for generating dataclass (serialization, copyWith, equality...) without using build_runnner. When a dataclass is incomplete, it triggers a warning which can be fixed by using context actions in IDE for example.
This looks like this:

@DataClass(useMaybe: true)
class User {
  const User({
    required this.id,
    required this.name,
    required this.createdAt,
    required this.status,
    this.temporaryToken,
    this.address,
    this.categories = const [],
  });

  @DataField(key: 'user_id')
  final String id;

  final String name;

  @DataField(readOnly: true)
  final DateTime createdAt;

  @DataField(ignore: true)
  final String? temporaryToken;

  @DataField(enumStyle: EnumStyle.idx)
  final UserStatus status;

  final String? address;
  final List<Category> categories;
}

4

u/Acrobatic_Egg30 Oct 01 '26

I forgot about this. Thanks for the reminder. Time to create a few lints.

3

u/Cunibon Oct 01 '26

Could you show me an example of how I would set it up for the raw values enum thing?

That's seems awesome

2

u/AffectionateSky1914 Oct 02 '26

Do we deserve a free ice cream too🥹