One-time Read Firebase Cloud (Dart/Flutter)
267
From the FutureBuilder doc :
The future must have been obtained earlier, e.g. during State.initState, State.didUpdateConfig, or State.didChangeDependencies. It must not be created during the State.build or StatelessWidget.build method call when constructing the FutureBuilder. If the future is created at the same time as the FutureBuilder, then every time the FutureBuilder's parent is rebuilt, the asynchronous task will be restarted.
Example :
Future<QuerySnapshot> future;
@override
void initState() {
super.initState();
future = Firestore.instance.collection("111").getDocuments();
}
// future : future
Author by
Alexander
Updated on December 24, 2022Comments
-
Alexander over 1 year
I need one-time read Data from Firebase Cloud, thats why I use FutureBuilder in my project (dart/flutter). But when the application is started it reads without stopping (as stream). What should I do to fix this?
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"]); }, ); } }, ), ); } }
-
Alexander over 3 yearsCould you write an example, please?
-
MickaelHrndz over 3 yearsthere you go :)
-
Alexander over 3 yearsOh, thanks) And one more question: it's ok, that firebase read data when application is closed on all devices?)))
-
MickaelHrndz over 3 yearsSorry, I don't understand the question.
-
Alexander over 3 yearsIn Firebase Analytics the number of reads is increased, although the application is closed on all devices.
-
MickaelHrndz over 3 yearsThat could be multiple things, from a simple delay to your Firebase being compromised. If it persists, create a new question if it doesn't already exist / change your password / regenerate keys.
-
Alexander over 3 yearsIt seems I understand what the problem is. Every time I refresh the page of Firebase Cloud in the browser the number of reads is increased after some minutes. It turns out that updating the page in the browser is considered as reading the data?