I am building an flutter application. there is signup windows and when user enters the mobile number it check whether it is already registered or not. But the problem is that the function is not waiting for the result from firebase.the function return true is false if there the user the user doesnot exist. But here no value is returned

import 'dart:async';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:zshoperretail/otp_verification.dart';

class mobilewindow extends StatelessWidget {
    TextEditingController mobileController=new TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.red,

      body: Center(
        child:Column(
          mainAxisAlignment: MainAxisAlignment.center,
            children:[
        ClipRRect(
        borderRadius: BorderRadius.circular(5),
        child:Container(
          height: 50,
          width: 300,
          color: Colors.white,
        child:TextField(
          maxLength: 10,
          maxLengthEnforced: true,
          controller: mobileController,
          keyboardType: TextInputType.number,
          decoration: InputDecoration(
            border: InputBorder.none,
            hintText:"Mobile Number",
            hintStyle: TextStyle(color: Colors.red),
          ),
        ),),),
        SizedBox(height: 5,),
        ElevatedButton(
          child: Text("Next",style: TextStyle(color: Colors.white),),
          onPressed:() =>saveMobile(context),
        )
            ])
      ),
    );

  }
  saveMobile(BuildContext context) async{
    var mobile= mobileController.text;
    mobile="+91"+mobile;
    var result1='';
    result1=await checkMobile(mobile);
    print('check1'+result1);
    if(result1=='false') {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      await prefs.setString('mobile', mobile);
      Navigator.push(
          context, MaterialPageRoute(builder: (context) => otp_verification()));
    }
    else{
      print(result1);
      showDialog(
          context: context,
          builder: (BuildContext context){
            return AlertDialog(
              title: Text("Error"),
              content: Text("Account already registered.please login."),
              actions: [
                FlatButton(
                  child: Text("Close"),
                  onPressed: (){
                    Navigator.of(context).pop();
                  },
                )
              ],
            );
          }
      );
    }
  }
  Future<String>checkMobile(String mobile) async{
   FirebaseFirestore firestore= FirebaseFirestore.instance;
   var result="";
   firestore.collection('retail_users').doc(mobile).get().then((DocumentSnapshot snapshot)async{
     if(snapshot.exists){
       print("hello");
       result='true';
     }
     else{
       print("hellno");
       result='false';
     }
   });
   return(result);
    }
}


Solution 1: Benyam S

when your call .then() function on a future object it will register the given function to be executed when the data arrive so it is not a good idea to put the result inside the callback function. Instead, remove the callback function inside .then() and use await.

DocumentSnapshot snapshot = await firestore.collection('retail_users').doc(mobile).get();

if(snapshot.exists){
  print("hello");
  result='true';
} else{
  print("hellno");
  result='false';
}

return(result);