gpt4 book ai didi

flutter - 如何手动刷新或重新加载 flutter firestore streambuilder?

转载 作者:IT王子 更新时间:2023-10-29 06:44:01 25 4
gpt4 key购买 nike

我的这个应用程序需要拉动刷新功能,所以我将 StreamBuilder Widget 放在 RefreshIndicator Widget 中,但我不知道如何手动刷新onRefreshed 事件被触发时的 StreamBuilder。

最佳答案

作为状态变量并在拉动刷新时重置将解决问题。

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

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 - 如何手动刷新或重新加载 flutter firestore streambuilder?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52132896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com