Flutter University
  • πŸ‘‹Welcome to Flutter University
  • Learn Flutter
    • πŸš—Basics
      • 🧺Dart Basics
      • πŸš›Setup & Installation
      • 🐣Hello Flutter
      • πŸŒ‰Widgets
      • ⛸️Basic State Management
      • πŸ‡ΎπŸ‡ΉBasic Layout and Styling
      • 🐝Basic Interactivity
      • πŸ›£οΈNavigation
      • πŸͺ„Working with Assets
    • πŸš…Intermediate
      • 🎯Deeper into Dart
      • ⭐More on State Management
      • πŸ“ƒForm Handling
      • πŸ—ΌNetworking
      • πŸŽ‡Persistence
      • πŸ§™β€β™‚οΈAnimations
      • πŸ§ͺTesting
      • πŸ“¦Package Management
    • ✈️Professional
      • πŸŽ“Advanced Animations
      • 🎨Custom Painters
      • 🐼Continuous Integration/Continuous Deployment (CI/CD)
      • 🎭Performance Profiling
      • πŸ”¬Native Integrations
      • 🌍Accessibility and Localization
      • 🀘Understanding Design Patterns
      • πŸ“šFlutter Architecture
        • The Layer Model
        • Reactive User Interfaces
        • Flutter Widgets
        • The Rendering Process
        • Platform Embedders Overview
        • Integrating with Other Code
        • Support for the Web
  • Tutorials
    • 🌈UI
      • 🏚️Clubhouse Clone
      • πŸ”‰Netflix Clone
    • βš”οΈFull Stack
    • ⛓️Blockchain
    • πŸ€–AI/ML
  • Miscellaneous
    • πŸ–₯️100 Days of Flutter
    • 🎨Join Community
Powered by GitBook
On this page
  • 1. Async/Await and Futures πŸ•’
  • 2. Streams 🌊
  • 3. Collections (List, Set, Map) πŸ—‚οΈ
  • 4. Error Handling πŸ›‘
  • 5. Custom Classes and OOP 🧩
  • Assignments πŸ“

Was this helpful?

  1. Learn Flutter
  2. Intermediate

Deeper into Dart

Dart is a rich language with many features. As we delve deeper, we'll cover some key concepts that will help you write more efficient and maintainable Flutter apps.

1. Async/Await and Futures πŸ•’

Dart has built-in support for asynchronous programming, which is crucial for IO-bound work like loading files or making network requests.

Future<String> fetchUserData() {
  // Simulate a network request
  return Future.delayed(Duration(seconds: 2), () => 'User Data');
}

void getUserData() async {
  String userData = await fetchUserData();
  print(userData);  // Prints: User Data
}

2. Streams 🌊

Streams provide a way to respond to a series of data over time, like user input or a file being read.

Stream<int> countStream(int max) async* {
  for (int i = 1; i <= max; i++) {
    yield i;  // Pauses execution, returns the value, then continues from here when resumed
    await Future.delayed(Duration(seconds: 1));
  }
}

void listenToStream() {
  countStream(5).listen((int value) {
    print(value);  // Prints 1, 2, 3, 4, 5 (each on a new line, once per second)
  });
}

3. Collections (List, Set, Map) πŸ—‚οΈ

Dart has powerful collection types like Lists, Sets, and Maps.

// List
List<String> fruits = ['apple', 'banana', 'cherry'];
fruits.add('date');

// Set
Set<String> colors = {'red', 'green', 'blue'};
colors.add('yellow');

// Map
Map<String, int> ages = {
  'Alice': 30,
  'Bob': 25,
};
ages['Charlie'] = 35;

4. Error Handling πŸ›‘

Handle errors gracefully using try-catch blocks.

void mightFail() {
  throw ('This function throws an error!');
}

void handleError() {
  try {
    mightFail();
  } catch (e) {
    print('Error: $e');
  }
}

5. Custom Classes and OOP 🧩

Leverage Object-Oriented Programming (OOP) principles to create custom classes.

class Person {
  String name;
  int age;

  Person(this.name, this.age);

  void greet() {
    print('Hello, my name is $name and I am $age years old.');
  }
}

void createPerson() {
  Person person = Person('Alice', 30);
  person.greet();  // Prints: Hello, my name is Alice and I am 30 years old.
}

Assignments πŸ“

Reinforce your learning with these exercises:

These topics are fundamental for working effectively with Flutter. By understanding these Dart concepts, you'll be well-equipped to tackle more complex tasks in your Flutter projects.

Up next, delve deeper into & State Management to learn how to create dynamic and responsive user interfaces!

Last updated 1 year ago

Was this helpful?

πŸš…
🎯