Flutter Chore: Telling your code not to repeat itself

How to easily limit the execution of a certain code-block to only a fixed amount of times.

Some of the most common things that you’d most certainly find in a production-level app, are code-blocks and functions that should be invoked/run for a single time or for a certain amount of times, and after that you want them to be ignored forever (or until the user decides to uninstall and install your app, or clear the app’s data, or delete the data folder, you get the idea).
For example, you obviously don’t want your on-boarding screens or tutorial pop-ups to appear every single time the user launches your app, unless you hate your users of course.. but I’ll assume that this is not the case.

So in this article i will introduce you to a flutter package that i have created and used in every production-level flutter app that I’ve worked on in the past 3 months, which made achieving the goals that I’ve talked about earlier, easier than ever.

Installing the package

To install the package, you need to add it to your pubspec.yaml file just like you add any flutter dependency:

dependencies:
flutter_chore: 0.0.4

And then run flutter pub get.

Initializing the package

Before we start using the package, we need to initialize it, to do this, import Flutter Chore with:

import 'package:flutter_chore/flutter_chore.dart';

And then Initialize the package like so:

await Chore.init();

Using the package

Now, here’s where the fun begins, to specify a code block to be invoked only a fixed amount of times, you have to register it as a new chore, and to do that, you have three options:

a. Using the Chore.newChore method:

The Chore.newChore method has 3 parameters..
mark: the unique mark for the chore (this is a string that is used to identify the chore)
times: the number of times to run the chore (defaults to 1)
F: the code block that is called only as many times as set in the times parameter, with the current time this code block is being called

for example:

Chore.newChore(
"unique_chore_mark",
(int time) {
// do something only 4 times
},
times: 4,
).run();

b. Using the Chore.builder method:

The Chore.builder method returns a builder to initiate a chain of methods to be called for building a new chore, you can specify the code block calling the invoke method, which has a callback parameter that provides the current time this block is being called, and then you can specify the times calling the times methods and the mark calling the mark method.

for example:

Chore.builder()
.invoke((int time) {
// do something only 4 times
})
.times(4)
.mark("unique_chore_mark")
.run();

other builder methods:

Chore.builder()
.invoke((int time) {
// do something only 1 time
})
.once()
.mark("unique_chore_mark")
.run();
Chore.builder()
.invoke((int time) {
// do something only 2 times
})
.twice()
.mark("unique_chore_mark")
.run();
Chore.builder()
.invoke((int time) {
// do something only 3 times
})
.thrice()
.mark("unique_chore_mark")
.run();

c. Using the once, twice, and thrice methods:

Same as the Chore.newChore method, but the times parameter is obsolete here, instead you can call the once, twice or thrice methods for the code to be invoked once, twice, or thrice.

Chore.once("unique_chore_mark", () {
// do something only 1 time
}).run();
Chore.twice("unique_chore_mark", (int time) {
// do something only 2 times
}).run();
Chore.thrice("unique_chore_mark", (int time) {
// do something only 3 times
}).run();

Listening for events:

In addition to specifying a code block that gets invoked every time, you can specify additional code blocks to be invoked at certain times, like before the last time, or if the block is already executed, you have three listener methods to achieve this:

onSecondTime: Used to set the code-block to be called when this is the second time the chore has been done.
beforeLastTime: Used to set the code-block to be called when the chore has only one time left to be done, and this is the time that is before the last time.
ifDone: Used to call a certain code-block if the main code-block was already executed as many times as specified previously, and there is nothing else to be done.

Chore.newChore(
"chore_mark",
(time) {
// do something only 5 times
},
times: 5,
).run().onSecondTime(() {
// do something before doing the chore for the second time
}).beforeLastTime(() {
// do something before doing the chore for the last time
}).ifDone(() {
// do something after the chore has been done as many times as set in the 'times' field
});

BONUS: Get a list of all the registered chores:

To get a list of all the chores that have been registered, you can use the Chore.getAllChores method, like so:

List<ChoreItem> chores = Chore.getAllChores();

And that’s it, if you’ve found this library useful, make sure to leave a start on the package’s repository on GitHub and to open an issue if you faced any problems, wanted to leave a suggestion or if you wanted to contribute.

Thank you for reading

Android developer, and the fastest "invalidate cache/restart" clicker in the world