Dart&Flutter with mizu
224 subscribers
22 photos
9 videos
20 links
Download Telegram
If you are using Dart 3.0 or later versions, do not forget to use the new list extensions when working with them.

What they are: nonNulls, firstOrNull, lastOrNull, singleOrNull, elementAtOrNull and indexed on Iterables.

It brings more functional code and no more index out-of-bounds issues when trying to access first, last, or finding elements.

#tip
6
If you are utilizing Bloc as a state management solution, it's crucial to avoid using multiple on statements in the constructor. Each on statement operates independently with its own queue and transformer without any specific order enforced. Doing so can lead to inconsistent states or race conditions.

Here are some tips for effectively handling events in Bloc:

- Try to use only one on event handler.
- Utilize pattern matching from freezed or sealed classes.
- Organize event handling using sequential transformer.
- Avoid using Cubit to handle complex state changes.

Clear explanation in comments...

#tip #bloc
3
Blocs and streams (emit.forEach)

Occasionally, we utilize blocs that observe a continuous flow of data. Typically, this flow originates from a repository within our domain layer, where it can be observed by several blocs. These blocs then leverage the data stream to inform their operational decisions based on business logic.

When dealing with streams in Bloc versions 7.2.0 and above, we can leverage the functionalities offered by the emit object to streamline our interaction with streams. The emit.forEach method enables us to receive a callback whenever there is data in the stream. Moreover, when our event handler is terminated or completes its task, it automatically handles the cleanup process by ending the stream subscription.

Here's an example illustrating how much emit.forEach method saves us from writing repetitive boilerplate code when utilizing stream in bloc.

#info #tip
4👍3
I’ve noticed that many devs believe they can only deconstruct object classes and records starting from Dart 3. However, Dart’s pattern matching you can also deconstruct nested maps and lists.
Map patterns match values that implement Map, and then recursively match its subpatterns against the map's keys to destructure them

A list pattern matches values that implement List, and then recursively matches its subpatterns against the list's elements to destructure them by position

This destructuring also works with complex nested data. For example, if your map contains other maps or complex data structures, you can easily unpack them.

Don’t forget to utilize this when needed to make your code more elegant.

#tip
5😍4👍2