I can't use wait in Dart with a method without async.

Asked 1 years ago, Updated 1 years ago, 62 views

*There was a mistake in recognition in the first post, so this is a correction

If you write data=wait readData() from the method with async, you will wait, but if you don't use the method, you will get an error saying that you can't use wait.
How can I use a method without async?

class FileTools{
  Future<Map<String, List<String>>readData() async {
    if(_file==null)setFile();

    try{
      String data = wait_file.readAsString();
      return json.decode(data);
    } catch(e){
      // If encounter an error, return 0.
      return null;
    }
  }
}

asynchronous flutter dart

2022-09-30 11:22

1 Answers

Determine the question as a way to invoke the asynchronous method readData() from one other asynchronous operation

Asynchronous processing is available in some programming languages with the word async/away, but away is used to call asynchronous functions from asynchronous functions and cannot be called from normal functions.(wait can be called without but cannot receive callback, etc.)

Dart and JavaScript have asynchronous processing capabilities prior to async/wait deployment and are available.
Asynchronous processing in Dart appears to be called future.To use it

The latter Future API specifies the then() method for callback acceptance(?) of asynchronous processing completion

final future=a_method()
future.then(val)=>print(val))

In addition, the Future API of Dart corresponds to Promise in JavaScript, so you may be able to refer to it
[Reference]
https://developer.mozilla.org/ja/docs/Web/JavaScript/Guide/Using_promises

doSomething()
.then(result=>doSomethingElse(result))
.then(newResult=>doThirdThing(newResult))
.then(finalResult=>{
  console.log(`Got the final result:${finalResult}`);
})
.catch(failureCallback);


2022-09-30 11:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.