Flutter Mobile Development · Lekcja

Programowanie asynchroniczne z Futures i async/await

Obsługuj opóźnione operacje w Dart i Flutter za pomocą Futures, async, await oraz obsługi błędów.

Lekcja 4 z 413 kroki

Programowanie asynchroniczne z Futures i async/await to bezpłatna lekcja Flutter Mobile Development na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Flutter Mobile Development, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Flutter Mobile Development zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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.

Bezpłatny start

Ucz się Dart dzięki korepetycjom AI — za darmo

Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.

Kursy
22
Lekcje
88

Często zadawane pytania

Czy lekcja „Programowanie asynchroniczne z Futures i async/await” jest bezpłatna?

Tak — pełny tekst „Programowanie asynchroniczne z Futures i async/await” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Flutter Mobile Development, przejdź na CoddyKit PRO. Kurs Flutter Mobile Development zawiera 4 lekcji w sumie.

Co nauczysz się w „Programowanie asynchroniczne z Futures i async/await”?

Obsługuj opóźnione operacje w Dart i Flutter za pomocą Futures, async, await oraz obsługi błędów. Ćwiczysz Flutter Mobile Development z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Flutter Mobile Development?

Nie wymagamy żadnego doświadczenia. Flutter Mobile Development w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Programowanie asynchroniczne z Futures i async/await”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Flutter Mobile Development?

Tak. Każda lekcja Flutter Mobile Development zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Ekosystem Fluttera i konfiguracja
  2. Podstawy języka Dart dla Fluttera
  3. Tworzenie pierwszej aplikacji Flutter
  4. Programowanie asynchroniczne z Futures i async/await
← Powrót do Flutter Mobile Development