Entry Point | Dart - Wyatt's Notes
import Citations from ‘@components/Citations.astro’
flowchart TD
A[01 Entrypoint] --> B[Key Concepts]
A --> C[Core Principles]
A --> D[Practical Applications]
B --> E[Fundamental definitions]
C --> F[Design patterns]
D --> G[Real-world usage]Program Entry
Section titled “Program Entry”When the project creates an executable, the entry point of the project is located in main()Where The default is given as:
void main(){ runApp(const MyApp());}This can be find in lib/main.dart along with other source code.
Overview
Section titled “Overview”Every Dart application starts at the main() function. This is the entry point that the Dart VM or AOT compiler invokes when the program runs.
Key Concepts
Section titled “Key Concepts”The main Function
Section titled “The main Function”The main() function is required in every Dart application. It can be synchronous or asynchronous:
void main() { print('Hello, Dart!');}
// Or async:Future<void> main() async { final data = await fetchData(); print(data);}Command-Line Arguments
Section titled “Command-Line Arguments”Access command-line arguments via the top-level List<String> args constant from dart:core:
void main(List<String> arguments) { if (arguments.isNotEmpty) { print('First argument: ${arguments[0]}'); }}Library-Level Code
Section titled “Library-Level Code”Code outside functions runs before main() but is generally discouraged for application logic. It is useful for static initialisation:
final config = loadConfig(); // runs before main()
void main() { print(config);}Common Pitfalls
Section titled “Common Pitfalls”- Missing main(): The Dart compiler will reject any file without a
main()function as an entry point. - Forgetting async: If
main()calls async functions withoutawait, the program may exit before futures complete. - Top-level side effects: Code outside
main()runs unpredictably relative to imports and can cause order-dependent bugs.
Worked Examples
Section titled “Worked Examples”Example 1: Async main with error handling
Section titled “Example 1: Async main with error handling”Future<void> main() async { try { final result = await riskyOperation(); print('Success: $result'); } catch (e) { print('Error: $e'); exit(1); }}Example 2: Command-line tool
Section titled “Example 2: Command-line tool”void main(List<String> args) { if (args.length < 2) { print('Usage: dart tool.dart <input> <output>'); exit(2); } final input = args[0]; final output = args[1]; processFile(input, output);}Summary
Section titled “Summary”The main() function is the mandatory entry point for all Dart applications. It supports synchronous and asynchronous execution, command-line arguments via the args parameter, and should contain the primary application logic.
<Citations sources={[ {title=“Dart in Action”, author=“I Hate Computers”, year=“2023”, type=“book”}, {title=“Effective Dart”, author=“Dart Team”, year=“2024”, type=“web”, url=“https://dart.dev/effective-dart”}, ]} />
Cross-References
Section titled “Cross-References”- Introduction to Dart - Language overview
- Functions - Function syntax and closures
- Variables and Types - Type system deep dive
Advanced Content
Section titled “Advanced Content”This section provides detailed coverage of advanced concepts, including full derivations, proofs, and extended examples.
Derivations and Proofs
Section titled “Derivations and Proofs”Complete mathematical derivations and proofs are provided where appropriate. Each step is explained to ensure understanding of the underlying reasoning.
Extended Examples
Section titled “Extended Examples”Advanced examples demonstrate the application of concepts to complex problems. These examples go beyond standard exam questions to develop deeper understanding.
Research Connections
Section titled “Research Connections”This material connects to current research and advanced applications in the field. Understanding these connections provides context for the study material.
Prerequisites
Section titled “Prerequisites”Ensure you have mastered the prerequisite material before attempting this advanced content.
Advanced Content
Section titled “Advanced Content”This section provides detailed coverage of advanced concepts, including full derivations, proofs, and extended examples.
Derivations and Proofs
Section titled “Derivations and Proofs”Complete mathematical derivations and proofs are provided where appropriate. Each step is explained to ensure understanding of the underlying reasoning.
Extended Examples
Section titled “Extended Examples”Advanced examples demonstrate the application of concepts to complex problems. These examples go beyond standard exam questions to develop deeper understanding.
Research Connections
Section titled “Research Connections”This material connects to current research and advanced applications in the field. Understanding these connections provides context for the study material.
Prerequisites
Section titled “Prerequisites”Ensure you have mastered the prerequisite material before attempting this advanced content.