如何手动刷新或重新加载Flutter Firestore StreamBuilder? [英] How to refresh or reload a flutter firestore streambuilder manually?

查看:174
本文介绍了如何手动刷新或重新加载Flutter Firestore StreamBuilder?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要拉动刷新功能的应用程序,因此我将 StreamBuilder窗口小部件放置在 RefreshIndicator窗口小部件中,但是我不知道如何手动刷新触发 onRefreshed 事件时,将其添加到StreamBuilder.

I have this application that needs a pull to refresh functionality, so I placed the StreamBuilder Widget inside the RefreshIndicator Widget, but I don't know how to manually refresh the StreamBuilder when the onRefreshed event is triggered.

推荐答案

stream作为state variable,并在上拉刷新时重置将解决此问题.

Having the stream as a state variable and resetting on pull on refresh will solve the problem.

在下面的代码中,我将在按下按钮时重置流.希望对您有帮助.

In below code, I am resetting the stream on button press. Hope that helps you.

import 'dart:async';

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new _MyAppState();
  }
}

class _MyAppState extends State<MyApp> {
  var stream; // state variable

  @override
  void initState() {
    super.initState();
    stream = newStream(); // initial stream
  }

  Stream<String> newStream() =>
      Stream.periodic(Duration(seconds: 1), (i) => "$i");

  @override
  Widget build(BuildContext context) {
    var streamBuilder = StreamBuilder(
        initialData: "0",
        stream: stream,
        builder: (context, snapshot) {
          return new Text(snapshot.data);
        });
    return MaterialApp(
        title: 'Trial',
        home: Scaffold(
            appBar: AppBar(title: Text('Stream builder')),
            body: Column(
              children: <Widget>[
                streamBuilder,
                FlatButton(
                    onPressed: () {
                      setState(() {
                        stream = newStream(); //refresh/reset the stream
                      });
                    },
                    child: Text("Reset"))
              ],
            )));
  }
}

这篇关于如何手动刷新或重新加载Flutter Firestore StreamBuilder?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆