Home > Back-end >  Break out of a loading flutter
Break out of a loading flutter

Time:09-05

I have a flutter code which allows user to search for every user speaking a specific language. I implemented a loading controlled by a 'isLoading' bool. The thing is, when the user searchs for a language that isn't used by any user, the loading becomes infinite. How to display a certain widget if the research doesn't find anything?

void onSeach() async {
setState(() {
  isLoading = true;
});
await DatabaseService()
    .firebaseFirestore
    .collection('users')
    .where('languages', arrayContains: _search.text)
    .get()
    .then((value) {
  setState(() {
    doces = value.docs;
    isLoading = false;
  });
});

}

CodePudding user response:

it's better to use FutureBuilder or StreamBuilder instead

FutureBuilder(
   future: onSeach(),
   builder: (context, snapshot) {
   if(if (snapshot.connectionState == ConnectionState.waiting) {
      return LoadingWidget();
    }
   if(snapshot.hasError) {
      return Text("some error happened");
   }      

   if(snapshot hasData) {
      return yourWidgets()}

   }
   }

CodePudding user response:

thanks for your question. I will suggest you add a Navigator.pop(context) immediately after the second setState of the call response like so:

    void onSeach() async {
setState(() {
isLoading = true;
});
await DatabaseService()
.firebaseFirestore
.collection('users')
.where('languages', arrayContains:     
 _search.text)
.get()
.then((value) {
 setState(() {
 doces = value.docs;
 isLoading = false;
  });
 Navigator.pop(context);
 });
  • Related