将 StreamBuilder 转换为 StreamProvider
convert StreamBuilder to StreamProvider
我有这样一个 StreamBuilder:
StreamBuilder<List<AuthInfo>>(
stream: myDatabase.authInfosDao.watch(),
builder: (context, snapshot) => (snapshot.data?.length ?? 0) > 0
? Home()
: Auth()),
我想将其重做为 Stream Provider,结果就是这样:
StreamProvider<List<AuthInfo>>.value(
value: myDatabase.authInfosDao.watch(),
initialData: [],
builder: (BuildContext context, AsyncSnapshop snapshot)...
我不知道如何使用“值”中的数据来加载所需的页面,因为 StreamProvider 中没有快照
StreamProvider<List<AuthInfo>>.value(
value: myDatabase.authInfosDao.watch(),
initialData: [],
child: Consumer<List<AuthInfo>>(builder: (context, data, _) {
}, );
每次新数据到达流时,StreamProvider 都会重建小部件。它的功能类似于 ChangeNotificationProvider,它不会生成新的更改放置更新提供程序正在侦听的数据。
dependencies:
flutter:
sdk: flutter
provider: ^6.0.2
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() {
runApp(
const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return
StreamProvider<ModelObject>(
initialData: ModelObject(value:0),
create:(_)=>createStream(),
child: MaterialApp(
home: Test_StreamProviderWidget(),
));
}
}
class ModelObject{
ModelObject({this.value});
int ? value;
}
Stream<ModelObject> createStream(){
return
Stream<ModelObject>.periodic( Duration(seconds:1), (currentPeriodicValue)=>ModelObject(value:currentPeriodicValue+1)).take(10000);
}
class Test_StreamProviderWidget extends StatefulWidget {
Test_StreamProviderWidget({Key? key}) : super(key: key);
@override
State<Test_StreamProviderWidget> createState() => _Test_StreamProviderWidgetState();
}
class _Test_StreamProviderWidgetState extends State<Test_StreamProviderWidget> {
@override
Widget build(BuildContext context) {
return Scaffold(appBar: AppBar(title:Text("stream provider")),body:
Consumer<ModelObject>(
builder:(context, model, child) {
return
Center(child:Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text('${model.value.toString()}',style: Theme.of(context).textTheme.headline2,)
],));
},
)
)
;
}
}
我有这样一个 StreamBuilder:
StreamBuilder<List<AuthInfo>>(
stream: myDatabase.authInfosDao.watch(),
builder: (context, snapshot) => (snapshot.data?.length ?? 0) > 0
? Home()
: Auth()),
我想将其重做为 Stream Provider,结果就是这样:
StreamProvider<List<AuthInfo>>.value(
value: myDatabase.authInfosDao.watch(),
initialData: [],
builder: (BuildContext context, AsyncSnapshop snapshot)...
我不知道如何使用“值”中的数据来加载所需的页面,因为 StreamProvider 中没有快照
StreamProvider<List<AuthInfo>>.value(
value: myDatabase.authInfosDao.watch(),
initialData: [],
child: Consumer<List<AuthInfo>>(builder: (context, data, _) {
}, );
每次新数据到达流时,StreamProvider 都会重建小部件。它的功能类似于 ChangeNotificationProvider,它不会生成新的更改放置更新提供程序正在侦听的数据。
dependencies:
flutter:
sdk: flutter
provider: ^6.0.2
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() {
runApp(
const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return
StreamProvider<ModelObject>(
initialData: ModelObject(value:0),
create:(_)=>createStream(),
child: MaterialApp(
home: Test_StreamProviderWidget(),
));
}
}
class ModelObject{
ModelObject({this.value});
int ? value;
}
Stream<ModelObject> createStream(){
return
Stream<ModelObject>.periodic( Duration(seconds:1), (currentPeriodicValue)=>ModelObject(value:currentPeriodicValue+1)).take(10000);
}
class Test_StreamProviderWidget extends StatefulWidget {
Test_StreamProviderWidget({Key? key}) : super(key: key);
@override
State<Test_StreamProviderWidget> createState() => _Test_StreamProviderWidgetState();
}
class _Test_StreamProviderWidgetState extends State<Test_StreamProviderWidget> {
@override
Widget build(BuildContext context) {
return Scaffold(appBar: AppBar(title:Text("stream provider")),body:
Consumer<ModelObject>(
builder:(context, model, child) {
return
Center(child:Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text('${model.value.toString()}',style: Theme.of(context).textTheme.headline2,)
],));
},
)
)
;
}
}