-1

I have a doubt, is there any way to make my application wait till the readings from firebase are complete?

for example:

Assuming that I want to get a value from firebase then I want to assign to variable, but when the application starts I don't want to see an empty screen, I could use a progress bar, but I didn't want a blank one.

       Future<String>change()async{
       await FirebaseFirestore.instance.collection('Users').where('mail',isEqual to 'test@gmail.com').get().then((value){
       value.docs.forEach((result){
        data1 = result.data()['rule'];
        });
       });
       }

      print (data);

if I print the data outside the method I will get null, How can I get a progress bar, so that the variable is only displayed after receiving the value from the database?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Luthermilla Ecole
  • 666
  • 1
  • 6
  • 14
  • 1
    you should learn a state management for flutter i can recommend you learning bloc https://bloclibrary.dev/#/ – Yaya Nov 26 '20 at 14:01

1 Answers1

1

I think you are searching for the Future.builder https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html

With this you can get a loading screen while your data is loading and when it is finished you can return your wanted build function.

Your code should look something like this:

Future changeFuture;

@override
  void initState() {
    super.initState();
    changeFuture = change();
  }

@override
  Widget build(BuildContext context) {
   return FutureBuilder(
          future: changeFuture,
          builder: (context, snapshot) {
            switch (snapshot.connectionState) {
              case ConnectionState.none:
                return Text('None');
              case ConnectionState.active:
                return Text('Active');
              case ConnectionState.waiting:
                return Text('Loading');
              case ConnectionState.done:
                return Container(Your content here)
              default:
                return Text('default')
justin0060
  • 73
  • 7