r/FlutterDev • • 21d ago

Discussion Dart’s if-case pattern is nice, but I replaced it with plain code. Where do you think it’s worth it?

Dart 3 gave us a lot of nice language features. One of them is the “if-case” pattern.

While working on my library, I had this code:

AdjustEvent toAdjustEvent(String eventToken) {
  final adjustEvent = parameters.toAdjustEvent(eventToken)..setRevenue(revenue, currency);
  if (deduplicationId case final id?) adjustEvent.deduplicationId = id;
  return adjustEvent;
}

It’s short. But to read it, you need to know Dart’s pattern rules. if (x case final id?) means “if x isn’t null, call it id and run this”.

I rewrote it in plain style:

AdjustEvent toAdjustEvent(String eventToken) => parameters.toAdjustEvent(eventToken)
  ..setRevenue(revenue, currency)
  ..deduplicationId = deduplicationId;

And here is the interesting part. When I looked closely, the null check wasn’t needed at all. The SDK field already accepts null and skips it when sending. So the plain version is not only easier to read, it’s also simpler.

The fancy line was protecting from something that couldn’t go wrong.

And this is really important when you write a library or design a public API. Your code will be read by many people with different Dart experience: users who open your source to understand what happens, and contributors who want to fix something. In app code only your team reads it, but in a library every clever line becomes a small barrier for someone else. Plain code makes your library easier to trust and easier to contribute to.

I still think patterns are great in some places, like checking JSON shape:

if (json case {'name': String name, 'age': int age}) {
  return User(name, age);
}

There it replaces several lines of checks and casts. But in my case it was just extra complexity.

Curious how you use them. Where do patterns really pay off for you, and do you write differently when the code is a library?

3 Upvotes

9 comments sorted by

8

u/raman4183 21d ago edited 21d ago

> if (x case final id?) means “if x isn’t null, call it id and run this”

Your interpretation is slightly off, “id?” represents the case when deduplicationId is nullable, bind it to a non-nullable variable called id and continue otherwise skip this if block.

> When I looked closely, the null check wasn’t needed at all. The SDK field already accepts null and skips it when sending. So the plain version is not only easier to read, it’s also simpler.

Naturally, your rewrite is simpler because
1 - your rewrite doesn’t have any conditional check.
2 - you were forcefully and unnecessarily checking a scenario which is possibly already handled by “AdjustEvent” setter internally.

2

u/_fresh_basil_ 21d ago

No, clearly if-case conditions are the issue here. /s

1

u/mateusfccp 19d ago

I love fresh basil, but it's so hard to take care of them in the climate I live in. They are so sensible to temperature changes.

1

u/_fresh_basil_ 19d ago

It's super easy to grow indoors in a hydroponic system!

Just water and nutrients and some grow lights, no dirt. It's nice because you can trim them to keep them as small as you want.

That's what I do, and I would recommend 10/10.

(You can even grow it in jars hydroponic style if you want)

1

u/mateusfccp 19d ago

I used to grow one outdoor in a vase and I had to take them inside on winter at night because less than 10 C and they burn.

It will also suffer with much heat, and here it gets to 40C at summer.

I now planted one in the dirt outside, let's see if I can make it survive at least until winter.

0

u/raman4183 18d ago

What the fuck are you guys talking about?

2

u/julemand101 21d ago edited 21d ago

Your interpretation is slightly off, “id?” represents the case when deduplicationId is nullable, bind it to a nullable variable called id and continue otherwise skip this if block.

Sorry but you are wrong here: https://dart.dev/language/pattern-types#null-check

The new variable is non-nullable since the id? pattern is a null-check.

Otherwise, this program would not be valid as we are calling .length on a potential null-variable if your interpretation would be the case:

import 'dart:math';

void main() {
  String? maybeString = Random().nextBool()
      ? 'nullable with base type String'
      : null;

  if (maybeString case final string?) {
    print(string.length);
  }
}

4

u/raman4183 21d ago

Hi, you are correct.

I said “nullabe variable” instead of “non-nullable”.

Thanks for pointing that out.