Mandip
Mandip

Reputation: 79

Chaing async method on Dart

I have following class.

class Element {
  Future<Element> findById(var id)async {
     await networkRequest();
     return this;
  }

 Futute<Element> click() async {
    await networkRequest();
    return this;
 }
}

I want to achieve the something like.

main() async {
  var element = Element();
  await element.findyById("something").click();
}

But I'm not able to do so because element.findById() returns future. How can I chain these async methods.

Upvotes: 1

Views: 671

Answers (3)

bereal
bereal

Reputation: 34281

While there's no special syntax to chain futures, there are two semantically equivalent ways to do what you want:

1) Two separate await calls:

await element.findById("something");
await click();

2) Chaining with then:

await element.findById("something").then(() => click());

Upvotes: 2

mezoni
mezoni

Reputation: 11210

final el = await element.findyById("something");
await el.click();

Upvotes: 0

ibrahimkarahan
ibrahimkarahan

Reputation: 3015

Use this,

await (await Element().findById("1")).click();

Upvotes: 1

Related Questions