我的BLoC实现有问题,我在synchronized.dart:中有这段代码
...
class _SynchronizeState extends State<Synchronize> {
UserBloc userBloc;
//final dbRef = FirebaseDatabase.instance.reference();
@override
Widget build(BuildContext context) {
userBloc = BlocProvider.of(context);
return Scaffold(
resizeToAvoidBottomPadding: false,
body: Container(
...
),
child: StreamBuilder(
stream: dbRef.child('info_tekax').limitToLast(10).onValue,
builder: (context, snapshot) {
if(snapshot.hasData && !snapshot.hasError){
Map data = snapshot.data.snapshot.value;
List keys = [];
data.forEach( (index, data) => keys.add(index) );
return ListView.builder(
itemCount: data.length,
itemBuilder: (context, index) => SynchronizeItem(title: keys[index], bottom: 10, onPressed: (){ print(keys[index]); })
);
}else{
return Container(
child: Center(
child: Text('Loading...'),
),
);
}
}
),
),
);
}
}
previos代码工作正常,但我想实现blockPattern,我有userBloc,然后我想放这个userBloc.getDevicesForSinchronized()
而不是dbRef.child('info_tekax').limitToLast(10).onValue,
我的问题是:
void getDevicesForSynchronized() {
return dbRef.child(DEVICES).limitToLast(10).onValue;
}
我收到此错误**无法从方法"getDevicesForSynchronized"返回"Stream"类型的vaue,因为它的返回类型为"void">
错误很明显,但我不知道我需要返回的类型是什么,请尝试:
Furure<void> getDevicesForSynchronized() async {
return await dbRef.child(DEVICES).limitToLast(10).onValue;
}
或
Furure<void> getDevicesForSynchronized() async {
dynamic result = await dbRef.child(DEVICES).limitToLast(10).onValue;
}
和另一个解决方案,但我不知道如何正确返回在StreamBuilder 中使用的值
从错误消息中可以看到返回类型为Stream。更改您的方法,如:
Future<Stream> getDevicesForSynchronized() async {
return dbRef.child(DEVICES).limitToLast(10).onValue;
}