An Inline function supports one line short syntax expression. Inline functions mostly used to perform single task. The way they execute will make them faster on any platform. So in today’s tutorial we would learn about How to Write Inline Arrow Functions in Dart Flutter With Example in Android iOS App. We would use the Arrow function with combination of Inline functions here.
Syntax:
1 |
Return Type + Function Name (Data Type Variable Name) => Statement ; |
Contents in this project How to Write Inline Arrow Functions in Dart Flutter With Example:
1. Open your project’s main.dart file and import material.dart package.
1 |
import 'package:flutter/material.dart'; |
2. Create void main runApp() method and here we would call our main MyApp class.
1 |
void main() => runApp(MyApp()); |
3. Creating our main class named as MyApp extends State less widget. This is our main View class.
1 2 3 4 5 |
class MyApp extends StatelessWidget { } |
4. Creating Inline Arrow function named as myName. The method is String return type. We would call this method in Text widget and pass a Text inside it. It will print the argument Text on screen.
1 |
String myName(String name) => 'Hello, $name!'; |
5. Creating Widget Build Area -> Material App Widget -> Scaffold Widget -> Center Widget -> Column Widget -> Text widget.
- Here we would call the myName() method with some random text and print the Text on Screen with function return message.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( myName('Flutter-Examples'), style: TextStyle(fontSize: 24, color: Colors.black), ) ], )))); } |
6. Complete source code for main.dart file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { String myName(String name) => 'Hello, $name!'; @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( myName('Flutter-Examples'), style: TextStyle(fontSize: 24, color: Colors.black), ) ], )))); } } |
Screenshot: