Asynchrone Programmierung mit Futures und async/await
Verarbeiten Sie verzögerte Vorgänge in Dart und Flutter mit Futures, async, await und Fehlerbehandlung.
Asynchrone Programmierung mit Futures und async/await ist eine kostenlose Flutter Mobile Development-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Flutter Mobile Development-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Flutter Mobile Development-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Why Async?
Apps fetch data, read files, and wait on timers. Blocking would freeze the UI, so Dart uses asynchronous programming to stay responsive.
The Future Type
A Future is a value that’ll arrive later. It’s either uncompleted, completed with a value, or completed with an error.
Future<String> fetchName() {
return Future.delayed(
Duration(seconds: 1),
() => "Ada",
);
}async and await
Mark a function async and use await to pause until a Future completes — async code that reads top-to-bottom like normal.
Future<void> main() async {
print("start");
String name = await fetchName();
print("Hello, " + name);
}await Suspends, Not Blocks
Key point: await suspends, it doesn’t block. The event loop keeps handling other work, so the UI stays responsive.
Returning Values
An async function always returns a Future. A plain return inside it just completes that Future with the value.
Future<int> doubleIt(int x) async {
return x * 2; // becomes Future<int>
}Error Handling
Wrap awaited calls in try/catch to handle failures — exactly like catching synchronous exceptions.
try {
var data = await fetchData();
} catch (e) {
print("Failed: " + e.toString());
}Chaining with then
Without async/await, you attach callbacks with then and catchError. Async/await is clearer but compiles to the same idea.
fetchName()
.then((n) => print(n))
.catchError((e) => print("error"));Running in Parallel
Use Future.wait to launch several Futures at once and await them together — faster than awaiting one after another.
var results = await Future.wait([
fetchA(),
fetchB(),
]);FutureBuilder in Flutter
The FutureBuilder widget rebuilds your UI as a Future goes from loading to done — so you show spinners and results declaratively.
FutureBuilder<String>(
future: fetchName(),
builder: (context, snapshot) {
if (!snapshot.hasData) return CircularProgressIndicator();
return Text(snapshot.data!);
},
)Streams: Many Values
A Future yields one value; a Stream yields many over time. Listen with await for or a StreamBuilder.
Stream<int> counter() async* {
for (var i = 0; i < 3; i++) {
yield i;
}
}Putting It Together
Putting it together: Futures for one-off results, async/await for clean code, try/catch for errors, and FutureBuilder to bind async data to widgets.
Quick Check
One quick check on Dart’s Future and async/await before you move on.
Recap
You’ve got Dart async: Future for later values, async/await for readable code, try/catch for errors, and Future.wait plus FutureBuilder.
Häufig gestellte Fragen
Ist die Lektion „Asynchrone Programmierung mit Futures und async/await“ kostenlos?
Ja — der vollständige Text von „Asynchrone Programmierung mit Futures und async/await“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Flutter Mobile Development-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Flutter Mobile Development-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Asynchrone Programmierung mit Futures und async/await“?
Verarbeiten Sie verzögerte Vorgänge in Dart und Flutter mit Futures, async, await und Fehlerbehandlung. Du übst Flutter Mobile Development mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Flutter Mobile Development zu starten?
Keine Vorkenntnisse erforderlich. Flutter Mobile Development auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „Asynchrone Programmierung mit Futures und async/await“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Flutter Mobile Development-Lektion Code schreiben und ausführen?
Ja. Jede Flutter Mobile Development-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Flutter-Ökosystem und Einrichtung
- Dart-Grundlagen für Flutter
- Entwicklung Ihrer ersten Flutter-App
- Asynchrone Programmierung mit Futures und async/await