import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:ui';
void main()=>runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
void initState() {
// TODO: implement initState
print('Create a Stream');
Stream<String> _stream=Stream.fromFuture(fetchData());//创建一个Stream,并且给Stream赋值;
_stream.listen(onData, onError:onError, onDone:onDone);//创建一个监听,包含三个监听方法;
print('Start Listning Stream');
super.initState();
}
//下面这个函数是给Stream赋值用的;
Future<String> fetchData()async{
await Future.delayed(Duration(seconds:3));
return 'Hello World';
}
//下面这三个方法是监视器内部包含的三种监听方法:
onData(String data){print('$data');}
onError(error){print('$error');}
onDone(){print('done');}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('为人民服务',style: TextStyle(fontSize: 18,fontWeight: FontWeight.bold),),
centerTitle: true,
),
);
}
}