Kinda Code
Home/Dart/Page 4

Dart

How to make Comments in Flutter & Dart

How to make Comments in Flutter & Dart

Updated: Mar 31, 2023
This is a concise article about commenting in Flutter and Dart. Single-Line Comment (Format Comment) Just put two slash symbols at the beginning of the line you want to comment out. // This is a......
Dart: Capitalize the First Letter of Each Word in a String

Dart: Capitalize the First Letter of Each Word in a String

Updated: Mar 31, 2023
The example below shows you how to capitalize the first letter of each word in a string (use a title case for each word in a string) in Dart (and Flutter as well). The code: // KindaCode.com void main() { String totTitle(String......
Dart: Sorting Entries of a Map by Its Values

Dart: Sorting Entries of a Map by Its Values

Updated: Mar 31, 2023
Imagine your boss gives you a Dart map like so: final Map<String, int> myMap = { 'a': 10, 'b': 20, 'c': 15, 'd': 5, 'e': 14 }; And then, he asks you to sort the map in the ascending/descending order of the......
How to Reverse/Shuffle a List in Dart

How to Reverse/Shuffle a List in Dart

Updated: Mar 31, 2023
The two examples below show you how to reverse and shuffle a given list in Dart. Reversing a Dart list Reversing a list means changing the order of the elements in the list so that the first element becomes the last, the second......
Dart: Calculate the Sum of all Values in a Map

Dart: Calculate the Sum of all Values in a Map

Updated: Mar 31, 2023
This short example-based 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: // main.dart void main() { const......
How to Reverse a String in Dart (3 Approaches)

How to Reverse a String in Dart (3 Approaches)

Updated: Mar 31, 2023
The three examples below show you three different ways to reverse a given string in Dart (e.g. turn ABC to CBA). Turn the String into an Array Example: // KindaCode.com // main.dart // Define a reusable function String......
How to Subtract two Dates in Flutter & Dart

How to Subtract two Dates in Flutter & Dart

Updated: Mar 31, 2023
In Flutter and Dart, you can subtract two dates by using the DateTime.difference method. The result is a duration. Example: void main() { final DateTime dateOne = DateTime(2022, 8, 30, 16, 59, 59); final DateTime dateTwo =......
Dart & Flutter: Convert a Duration to HH:mm:ss format

Dart & Flutter: Convert a Duration to HH:mm:ss format

Updated: Mar 31, 2023
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: HH: hours (If it is less than 10, there will be a preceding zero) mm: minutes as 2 digits from 00 to......
Flutter & Dart: How to Check if a String is Null/Empty

Flutter & Dart: How to Check if a String is Null/Empty

Updated: Mar 31, 2023
When working with Flutter and Dart, there might be cases where you have to check whether a given string is null or empty. We can define a reusable function to do the job as follows: bool validateInput(String? input) { if (input ==......
Dart: Using Async and Await in Loops

Dart: Using Async and Await in Loops

Updated: Mar 31, 2023
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, it will wait......
Dart: Converting a List to a Set and vice versa

Dart: Converting a List to a Set and vice versa

Updated: Mar 31, 2023
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 a list into a set and vice versa, turn a set......
Flutter & Dart: Count Occurrences of each Element in a List

Flutter & Dart: Count Occurrences of each Element in a List

Updated: Mar 31, 2023
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 best strategy to get the job done is to use a map to store the frequency of each element in the list......