Flutter Mobile Development · บทเรียน

สตรีมและข้อมูลแบบรีแอกทีฟ

เรียนรู้การทำงานกับ Dart Streams ใน Flutter เพื่อจัดการข้อมูลแบบไม่พร้อมกันต่อเนื่อง เช่น การอัปเดตสด ซ็อกเก็ต และ UI แบบรีแอกทีฟ

บทเรียน 4 จาก 413 ขั้นตอน

สตรีมและข้อมูลแบบรีแอกทีฟ เป็นบทเรียน Flutter Mobile Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Flutter Mobile Development และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Flutter Mobile Development มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Beyond a Single Future

A Future gives you one value later. A Stream gives you many values over time.

  • WebSocket messages
  • Sensor readings
  • Live database updates

Streams are the reactive backbone of async Dart.

Listening to a Stream

Subscribe to a stream with listen, supplying callbacks for data, errors and completion.

stream.listen(
  (data) => print('Got: ' + data.toString()),
  onError: (e) => print('Error: ' + e.toString()),
  onDone: () => print('Done'),
);

Creating Streams with async*

An async generator uses async* and yield to emit values.

Stream<int> countTo(int n) async* {
  for (var i = 1; i <= n; i++) {
    await Future.delayed(const Duration(seconds: 1));
    yield i;
  }
}

await for

Inside an async function you can iterate a stream with await for, processing each value as it arrives.

Future<void> run() async {
  await for (final value in countTo(3)) {
    print(value);
  }
}

Single vs Broadcast

A single-subscription stream allows one listener. A broadcast stream allows many. Convert with asBroadcastStream().

final broadcast = controller.stream.asBroadcastStream();
broadcast.listen((v) => print('A: ' + v.toString()));
broadcast.listen((v) => print('B: ' + v.toString()));

StreamController

A StreamController lets you push values into a stream from your own code.

final controller = StreamController<String>();
controller.add('hello');
controller.add('world');
controller.close();

Transforming Streams

Streams have functional operators like map, where and take.

stream
  .where((n) => n.isEven)
  .map((n) => n * 10)
  .take(5)
  .listen(print);

StreamBuilder Widget

In the UI, StreamBuilder rebuilds whenever the stream emits a new value.

StreamBuilder<int>(
  stream: countTo(5),
  builder: (context, snapshot) {
    if (!snapshot.hasData) return const CircularProgressIndicator();
    return Text('Value: ' + snapshot.data.toString());
  },
);

Reading the Snapshot

The AsyncSnapshot tells you the connection state and whether data or an error is present.

  • snapshot.connectionState
  • snapshot.hasData / snapshot.data
  • snapshot.hasError / snapshot.error
if (snapshot.hasError) return Text('Error: ' + snapshot.error.toString());

Cancelling Subscriptions

Always cancel subscriptions in dispose to avoid memory leaks.

late StreamSubscription sub;

@override
void dispose() {
  sub.cancel();
  super.dispose();
}

Backpressure & Errors

Streams can emit errors mid-flow. Handle them with handleError so one bad event does not kill the whole pipeline.

stream
  .handleError((e) => print('Skipped: ' + e.toString()))
  .listen(print);

Quick Check

Which widget rebuilds your UI automatically as a stream emits new values?

Recap

You learned to work with Streams:

  • async* / yield and StreamController to create them
  • map / where / take to transform
  • StreamBuilder to drive reactive UI
  • Cancelling subscriptions and handling errors

Streams let your app react to continuous async data in real time.

เริ่มต้นได้ฟรี

เรียนรู้ Dart ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
22
บทเรียน
88

คำถามที่พบบ่อย

บทเรียน “สตรีมและข้อมูลแบบรีแอกทีฟ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “สตรีมและข้อมูลแบบรีแอกทีฟ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flutter Mobile Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flutter Mobile Development มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “สตรีมและข้อมูลแบบรีแอกทีฟ”

เรียนรู้การทำงานกับ Dart Streams ใน Flutter เพื่อจัดการข้อมูลแบบไม่พร้อมกันต่อเนื่อง เช่น การอัปเดตสด ซ็อกเก็ต และ UI แบบรีแอกทีฟ คุณปฏิบัติ Flutter Mobile Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flutter Mobile Development หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flutter Mobile Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “สตรีมและข้อมูลแบบรีแอกทีฟ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Flutter Mobile Development นี้ได้ไหม

ได้ บทเรียน Flutter Mobile Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. Futures และ Async/Await
  2. คำขอ HTTP และ JSON
  3. การจัดการข้อผิดพลาดแบบอะซิงโครนัส
  4. สตรีมและข้อมูลแบบรีแอกทีฟ
← กลับไปที่ Flutter Mobile Development