Important Functions in Flutter
Collins Mahigi
?? Full-Stack Software Engineer | UI/UX Designer | React.js, Vue.js, Angular, Node.js, Express.js, Django, Flask | Flutter, Dart, Kotlin, Swift | AWS, Docker | REST APIs, GraphQL, MySQL, MongoDB | Cloud & DevOps Expert
In Flutter, Dart functions play a crucial role in managing UI updates, handling state, and improving performance. Here are some of the most important Dart functions you should know when developing Flutter apps:
1?? UI & Widget Lifecycle Functions
? setState(() { ... }) – Updates the UI by notifying the framework that state has changed.
? initState() – Called once when a stateful widget is inserted into the tree, great for initializing data.
? dispose() – Used to clean up resources (e.g., closing streams, controllers) when a widget is removed.
? didChangeDependencies() – Called when inherited widgets change and require updates. ? build(BuildContext context) – The core function that builds the UI of a widget.
2?? List & Collection Functions
? map() – Transforms a list into another format.
?? Example:
List<int> numbers = [1, 2, 3];
List<int> squared = numbers.map((n) => n * n).toList(); // [1, 4, 9]
? where() – Filters a list based on conditions.
? reduce() – Combines all elements of a list into a single value.
? contains() – Checks if a list contains a specific value.
3?? String & Number Functions
? toString() – Converts a number/object to a string.
? int.parse() & double.parse() – Converts a string to an integer or double.
? toUpperCase() & toLowerCase() – Changes the case of a string.
? substring(start, end) – Extracts part of a string.
? replaceAll(old, new) – Replaces parts of a string.
领英推荐
4?? Date & Time Functions
? DateTime.now() – Gets the current date & time.
? DateTime.parse() – Converts a string into a DateTime object.
? difference() – Calculates the time difference between two dates.
5?? Future & Async Functions (Asynchronous Programming)
? async & await – Used for handling asynchronous operations.
? Future.delayed(Duration(seconds: x)) – Delays execution of code.
? then() – Executes a function after a Future completes.
?? Example:
Future<String> fetchData() async {
await Future.delayed(Duration(seconds: 2));
return "Data Loaded!";
}
6?? Error Handling Functions
? try-catch – Catches errors and prevents app crashes.
? on Exception catch (e) – Catches specific exceptions.
? assert() – Used for debugging, throws errors if conditions aren’t met.
7?? Navigation Functions
? Navigator.push() – Moves to a new screen.
? Navigator.pop() – Closes the current screen and goes back.
? Navigator.pushReplacement() – Replaces the current screen with a new one.
8?? State Management & Dependency Injection
? Provider.of<T>(context) – Retrieves the nearest provider in Provider state management. ? context.read<T>() – Reads a value without rebuilding UI.
? context.watch<T>() – Watches for state changes and rebuilds UI.