2

Basically I need to read a file as string synchronously in flutter declaring variable with imports. My reason for this is because I'm using the graphql widget which needs the query string. I want to have these in their graphql files so that I get vscode intellisense and validation against my schema and I don't want asynchronous code in my build method.

I tried using darts File().readAsStringSync(), but it seems to not be able to resolve the relative paths. I know there's path_provider plugin but that can only resolve the app path async.

Ryan Kauk
  • 51
  • 3
  • You simply need to get the path before building that widget, let's say before pushing the route. There is no difference in functionality between `readAsString` and `readAsStringSync`.. – creativecreatorormaybenot Aug 17 '19 at 16:22
  • 2
    @creativecreatorormaybenot Yea they do the same it's just one is sync and the other is future. I could use getApplicationDocumentsDirectory() from path_provider in the main function, but then I would have to store that path in state and still have to hard code the path to my graphql file relative to the base app directory. It just seems overly complicated to simply just read a file as text. And since the only way to resolve application path is by using the path_provider plugin and all the functions are async, I cannot just simply place them as const along with import statements. – Ryan Kauk Aug 18 '19 at 05:16

1 Answers1

0

By definition, synchronous operation executes requests one at a time. On the other hand, asynchronous operation executes requests simultaneously. If you'd like to execute operations synchronously, you can use the await keyword to wait for the current operation to finish before proceeding to the next.

Future<void> yourRequest(){
  await firstRequest();
  await secondRequest();
}

Future<void> firstRequest(){
  ...
}

Future<void> secondRequest() {
  ...
}
Omatt
  • 8,564
  • 2
  • 42
  • 144