简体   繁体   English

如何创建 Flutter stream 构建器?

[英]How do I create Flutter stream builder?

I have an "updateDatabase" function that retrieves the data from the Firebase Real-Time Database and saves it in the shared preferences... I put this function in the Init State of my home page but I would like this to become a stream builder so as not to having to reopen the page each time to get updated data but have them immediately. I have an "updateDatabase" function that retrieves the data from the Firebase Real-Time Database and saves it in the shared preferences... I put this function in the Init State of my home page but I would like this to become a stream builder这样就不必每次都重新打开页面来获取更新的数据,而是立即拥有它们。

This is part of my updateDatabase function:这是我的更新数据库 function 的一部分:

updateDatabase() async {
  final prefs = await SharedPreferences.getInstance();

  DatabaseReference starCountRef =
      FirebaseDatabase.instance.ref('/utenti/$uid/');
  starCountRef.onValue.listen((DatabaseEvent event) {
    final data = event.snapshot.value as Map;
    prefs.setInt("videoAgosto2022", data["videoAgosto2022"]);
ect...
}

This is part of my home page...这是我主页的一部分...

Widget build(BuildContext context) { double altezza = MediaQuery.of(context).size.height;小部件构建(BuildContext context){ double altezza = MediaQuery.of(context).size.height;

  return StreamBuilder(
        stream: What should I put here?,
        builder: (context, snapshot) => SingleChildScrollView(
              child: Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Column(

What should I put in the "dada" field?我应该在“dada”字段中输入什么?

A stream function must be marked as async* A stream function 必须标记为async*

Then, you just have to put your function in the stream attribute of the StreamBuilder widget然后,您只需将 function 放入StreamBuilder小部件的stream属性中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何修改代码以获得 Stream<dynamic> 为 stream 生成器键入 stream?</dynamic> - How do i modify the code to have a Stream<dynamic> type stream for stream builder? Stream builder 从 firestore 到 flutter - Stream builder from firestore to flutter 如何在 flutter stream 生成器中使用不同的 firestore 集合创建 where 条件? - How to make where condition with different firestore collection in flutter stream builder? 如何监听stream和更新Flutter GridView builder访问Firebase - How to listen to stream and update Flutter GridView builder accessing Firebase 如何使用 stream 构建器正确读取数据。 我不断收到空白页 - How do I read data with stream builder correctly. I keep getting blank page 如何在 flutter firebase 中创建进度条 - how do I create a progress bar in flutter firebase Flutter Stream 生成器抛出错误 null,我无法使用 Doc() 访问当前用户 - Flutter Stream builder throwing an Error of null and i cannot access current user using Doc() 如何使 stream 构建器购物车项目可通过触摸滚动? - How can I make stream builder cart items scrollable by touch? 如何在Flutter中过滤一个stream? - How can I filter a stream in Flutter? Flutter Stream Builder with Firestore 返回空列表 - Flutter Stream Builder with Firestore return empty list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM