What does the => arrow syntax do?
Arrow syntax is shorthand for { return expression; }
Write reusable code with functions
Passing the Quiz & Exercises is mandatory to earn FX and proceed. If you fail, try again to gain the required FX, then the Proceed button will become available.
You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.
Having trouble? AI is here to help
Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.
Use the AI button at the bottom of the screen
Functions are reusable blocks of code that you can call from anywhere in your program. Here is the basic syntax.
1// Basic function2String greet(String name) {3return 'Hello, $name!';4}56// Same function using arrow syntax (shorter form):7// String greet(String name) => 'Hello, $name!';89// Void function (returns nothing)10void printMessage(String msg) {11print(msg);12}
Dart supports two types of optional parameters that make your functions more flexible.
1// Named parameters (use curly braces)2void createUser({required String name, int age = 18}) {3print('$name is $age years old');4}5createUser(name: 'John', age: 25);6createUser(name: 'Jane'); // age defaults to 1878// Positional optional parameters (use square brackets)9void greet(String name, [String greeting = 'Hello']) {10print('$greeting, $name!');11}12greet('John'); // Hello, John!13greet('John', 'Hi'); // Hi, John!
Most Flutter widgets use named parameters. That is why you see `Text("Hi", style: TextStyle(...))` everywhere.
Anonymous functions are functions without names. They are commonly used for callbacks and event handlers in Flutter.
1// In a list2var numbers = [1, 2, 3, 4, 5];34// Anonymous function5numbers.forEach((n) {6print(n * 2);7});89// Arrow syntax (shorter)10numbers.forEach((n) => print(n * 2));1112// Common in Flutter13ElevatedButton(14onPressed: () {15print('Button clicked!');16},17child: Text('Click me'),18)
Good functions are small, focused, and predictable.
When code becomes hard to test, it is usually a function design problem first.
Write reusable code with functions. Dart is the language behind Flutter, and clear Dart code makes your Flutter UI easier to maintain.
Build confidence with basics first; speed comes later.
Prefer readable names, explicit intent, and async/await over deeply nested callbacks.
When Dart basics are weak, state bugs, null crashes, and async confusion increase quickly.
Arrow syntax is shorthand for { return expression; }
Named parameters with defaults use that default when not provided.
Use required before the type to make a named parameter mandatory.
Dart uses () {} or () => expression for anonymous functions (lambdas).
Something seems incorrect, unclear, or missing? Let us know.
FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.
Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.
Sign in to continue with exercises and quiz for this lesson.
Sign In to Continue