简体   繁体   中英

how do i force Flutter to run all the lines inside the function?

I have a this function that I need to run during initstate() in its entirety before building the widget. I have used this kind of code before in some parts of my app and it works there, but in this case, flutter jumps out before executing .then , goes to build the widget tree, and then returns back to the function but skips the remaining lines. I'm confused where I should properly put async-awaits to force it to finish the block. Also, can I ask where I can read an explanation of the proper flow of execution for flutter so that I can understand it more?

Future <bool> checkVendorStatus (buyerId) async {


var _result;

var  vendorDocRef = await buyersInfoColl.doc(buyerId)
    .collection("vendorsCalled")
    .doc(auth.currentUser!.uid)
    .get()
    .then((value) async {
        return await value.exists ? _result = true : _result = false;
    }

);
return _result;

await is meant to interrupt the process flow until the async method has finished. then however does not interrupt the process flow (meaning the next instructions will be executed) but enables you to run code when the async method is finished.

you can write your code like this-

Future <bool> checkVendorStatus (buyerId) async {


var _result;

var  vendorDocRef = await buyersInfoColl.doc(buyerId)
.collection("vendorsCalled")
.doc(auth.currentUser!.uid)
.get();
    
 vendorDocRef.exists ? _result = true : _result = false;



return _result;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM