Dart: Calculate the Sum of all Values in a Map
This short article shows you a few ways to find the sum of all values in a given map in Dart (presume that all the values are numbers). Using a For/In loop Example: Output: Using Reduce()…
How to Reverse a String in Dart (3 Approaches)
The three examples below show you three different ways to reverse a given string in Dart (e.g. turn ABC to CBA). Example 1 Output: Example 2 Output: Example 3 Output: Futher reading: How to Subtract two…
How to Subtract two Dates in Flutter & Dart
In Flutter and Dart, you can subtract two dates by using the DateTime.difference method. The result is a duration. Example: Output: The DateTime class also has a method named subtract. However, this one isn’t used for…
Dart & Flutter: Convert a Duration to HH:mm:ss format
When working with Dart and Flutter, there might be cases where you want to turn a Duration into a string of HH:mm:ss format: mm: minutes as 2 digits from 00 to 59 ss: seconds as 2…
Flutter & Dart: How to Check if a String is Null/Empty
When working with Flutter and Dart, there might be cases where you have to check whether a given string is null or empty. Suppose that our app’s functionality only continues when the string is neither null…
Dart: Using Async and Await in Loops
In Dart (and Flutter as well), you can perform synchronous operations sequentially in loops by using Future.forEach. The example program below will print the numbers from 1 to 10. Every time it finishes printing a number,…
Dart: Converting a List to a Set and vice versa
In Dart, a list is an indexable collection of objects with a length while a set is a collection of objects where each object can occur only once. This short article shows you how to convert…
Flutter & Dart: Count Occurrences of each Element in a List
When working with Dart and Flutter, there might be cases where you want to count the occurrences of each element in a given list. The simple example below will demonstrate how to do that: Output: That’s…
Flutter & Dart: Convert a String into a List and vice versa
In Flutter and Dart, you can turn a given string into a list (with string elements) by using the split() method. Example: Output: To convert a list of strings to a string, you can use the…
Dart: Extract a substring from a given string (advanced)
This article is about extracting a substring from a given string in Dart (and Flutter as well). We’ll cover the fundamentals, write a simple program, then walks through a few advanced use cases. Without any further…
Dart: Check whether a string starts/ends with a substring
To check if a string starts with or ends with a given substring in Dart (and Flutter as well), you can use the startsWith() method and the endsWith() method, respectively: These methods are case-sensitive (distinguish between…
Dart: Convert Class Instances (Objects) to Maps and Vice Versa
This article walks you through a few examples of converting a class instance (object) into a map and vice versa in Dart (and Flutter as well). Learning through examples is the best way to deepen understanding…