0% found this document useful (0 votes)
15 views12 pages

Lesson4 - Asynchronous in Dart

The document discusses advanced Dart programming concepts, focusing on asynchronous programming, Futures, async/await, and Streams. It emphasizes the importance of handling errors and provides best practices for managing asynchronous tasks in Flutter applications. Key features include using async/await for readability, handling multiple values with Streams, and ensuring error management in both Futures and Streams.

Uploaded by

Loki Legends
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views12 pages

Lesson4 - Asynchronous in Dart

The document discusses advanced Dart programming concepts, focusing on asynchronous programming, Futures, async/await, and Streams. It emphasizes the importance of handling errors and provides best practices for managing asynchronous tasks in Flutter applications. Key features include using async/await for readability, handling multiple values with Streams, and ensuring error management in both Futures and Streams.

Uploaded by

Loki Legends
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

ADVANCED DART

PROGRAMMING

FLUTTER
• Dart is a single-threaded language, meaning it
executes code sequentially. However, to keep
applications responsive (especially Flutter apps), we
use asynchronous programming to handle tasks
ASYNCHRONOUS
like:
• Fetching data from a server
PROGRAMMING
• Reading files
• Waiting for user input
FUTURES IN DART
• A Future represents a value that might not be available yet but will be in the future.

Future<String> → This function


returns a Future that will eventually
provide a String value.

Future.delayed(Duration(seconds: 2), ()
=> "Data loaded") → This simulates a
delayed operation (e.g., fetching data
from a network or database).

The .then((data) {...}) → method


executes once the Future completes,
printing "Data loaded“, .then() is used to
handle the result of the Future when it
completes.
HANDLING ERRORS IN FUTURES

.catchError() is used to handle errors in


Futures.

fetchDataWithError() where the catch


error are thrown
ASYNC/AWAIT IN DART
• async/await allows us to write asynchronous code in a way that looks like normal sequential (synchronous) code,
making it easier to read and understand.

async → Marks the function as


asynchronous, allowing it to use await
inside.

The await keyword pauses execution of


fetchData() until the delay completes.
HANDLING ERRORS WITH TRY-CATCH
• async/await allows us to write asynchronous code in a way that looks like normal sequential (synchronous) code,
making it easier to read and understand.

await makes the async code look


synchronous.

try-catch is used to handle errors in


async functions.

If an async function throws an error,


execution jumps to catch immediately.

Prevents app crashes by gracefully


handling errors.
STREAM IN DART
• A Stream in Dart is like a pipeline that delivers multiple asynchronous data events over time. Unlike a Future, which
gives you a single value once, a Stream can keep emitting values continuously.
async* → Declares a function that returns a Stream.

yield → Emits a value to the stream (instead of return).

Future.delayed(Duration(seconds: 1)) → Simulates asynchronous delay before emitting the next value.

.listen((value) {...}) → Subscribes to the stream and receives each emitted value.
HANDLING STREAM ERRORS

onError: (error) Runs when an error occurs


in the stream.

onDone: () Runs when the stream finishes


(but in this case, it stops at error).
EFFECTIVE MANAGEMENT OF ASYNCHRONOUS TASKS
• Futures – Handles a single result that completes in the future.
• async/await – Makes asynchronous code look synchronous for readability.
• Streams – Handles multiple values over time (e.g., user events, API responses).
BEST PRACTICES:
• Use async/await for better readability instead of .then().
• Use Future.wait() for parallel execution of multiple async tasks.
• Use Stream for handling multiple values over time.
• Always handle errors in Futures (try-catch) and Streams (onError).
• Cancel unused Streams to free up resources.
THANK YOU ☺

RYAN FERMO

You might also like