-
创建和监听,暂停恢复/取消监听
Stream _streamDemo = Stream.fromFuture(fetchData());
_streamDemo.listen(onData, onError: onError, onDone: onDone); -
添加订阅
-
StreamController
控制管理Stream
使用 Sink 往 Stream 上添加数据
创建可以多次订阅的 Broadcast Streams -
StreamBuilder
根据 Stream 上的数据构建小部件
import 'dart:async';
import 'package:flutter/material.dart';
class StreamDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('StreamDemo'),
elevation: 0.0,
),
body: StreamDemoHome(),
);
}
}
class StreamDemoHome extends StatefulWidget {
@override
_StreamDemoHomeState createState() => _StreamDemoHomeState();
}
class _StreamDemoHomeState extends State<StreamDemoHome> {
StreamSubscription _streamDemoSubscription;
StreamController<String> _