gpt4 book ai didi

android - 一次性读取Firebase云( Dart/flutter )

转载 作者:行者123 更新时间:2023-12-03 03:50:21 25 4
gpt4 key购买 nike

我需要从Firebase Cloud中一次性读取数据,这就是为什么我在项目中使用FutureBuilder( Dart / flutter )的原因。但是,当应用程序启动时,它的读取不会停止(作为流)。我应该怎么做才能解决这个问题?

class Hello extends StatefulWidget {
@override
_HelloState createState() => _HelloState();
}

class _HelloState extends State<Hello> {

Future getPosts() async{
QuerySnapshot qn = await FirebaseFirestore.instance.collection("111").get();
return qn.docs;
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: new Text('Hello'),
),
body: FutureBuilder(
future: getPosts(),
builder: (context, snapshot){
if(snapshot.connectionState == ConnectionState.waiting){
return Center(
child: CircularProgressIndicator(),
);
}
else{
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index){
return Text(snapshot.data[index].data()["subject"]);
},
);
}
},
),
);
}
}

最佳答案

FutureBuilder doc:

The future must have been obtained earlier, e.g. duringState.initState, State.didUpdateConfig, orState.didChangeDependencies. It must not be created during theState.build or StatelessWidget.build method call when constructing theFutureBuilder. If the future is created at the same time as theFutureBuilder, then every time the FutureBuilder's parent is rebuilt,the asynchronous task will be restarted.


范例:
  Future<QuerySnapshot> future;

@override
void initState() {
super.initState();
future = Firestore.instance.collection("111").getDocuments();
}

// future : future

关于android - 一次性读取Firebase云( Dart/flutter ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63757635/

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