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
Share:
267
Alexander
Author by

Alexander

Updated on December 24, 2022

Comments

  • Alexander
    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
    Alexander over 3 years
    Could you write an example, please?
  • MickaelHrndz
    MickaelHrndz over 3 years
    there you go :)
  • Alexander
    Alexander over 3 years
    Oh, thanks) And one more question: it's ok, that firebase read data when application is closed on all devices?)))
  • MickaelHrndz
    MickaelHrndz over 3 years
    Sorry, I don't understand the question.
  • Alexander
    Alexander over 3 years
    In Firebase Analytics the number of reads is increased, although the application is closed on all devices.
  • MickaelHrndz
    MickaelHrndz over 3 years
    That 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
    Alexander over 3 years
    It 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?