[Solved] How do I ensure a specific execution order when forced to use Dart async/await

Question

Asked by Twelve1110 on December 14, 2021 (source).

I am writing a basic programme to teach myself Dart/Flutter. Part of the programme uses the http.dart package to get some data and the http.get command returns a Future value. In order to unpack this value, I need to use an await command, which then changes the execution order of my code. I cannot work out how to preserve the intended execution order whilst using async/await. I am new to this, so appreciate that I am probably missing something obvious.

Code example 1 below uses async/await through a series of functions. This approach gives more or less the correct output order (other than the end of main()), but would mean (I think) that I would need to have an async build() method, which is not valid in Flutter.

// Cascading async methods with local variables and await statements
import 'dart:async';
import 'dart:math';
import 'package:http/http.dart' as http;

void main(List<String> arguments) {
  print('Main: start.');
  _build();
  print('Main: end.');
}

// Draw some stuff, make some decisions
void _build() async {
  print('build: Before getName.');
  String name = await _getName();
  print('build: After getName.');
}

// Get some data, make some decisions
Future<String> _getName() async {
  print('getName: before getData');
  String name = await _getData();
  print('getName: after getData');
  double val = Random().nextDouble();
  if (val < 0.5) {
    print('getName: returning body.data');
    return name;
  } else {
    print('getName: returning Bob');
    return 'Bob';
  }
}

// Get the data via an http request
Future<String> _getData() async {
  print('getData: Before http get.');
  final data = await http.get(Uri.parse('http://www.google.co.uk'));
  print('getData: After http get.');
  return data.body;
}

The output from this is (I have truncated the html data that is returned):

Main: start.
build: Before getName.   
getName: before getData  
getData: Before http get.
Main: end.
getData: After http get.
getName: after getData
getName: returning body.data
build: After getName.  Name is: <html data>

The second code example below uses a global variable to capture data in the _getName() method so that I can avoid using async/await in the build() method. This does not give the correct execution order or the correct output.

// Use global variable to receive awaited data and avoid cascading async methods
import 'dart:async';
import 'dart:math';
import 'package:http/http.dart' as http;

String name = "";

void main(List<String> arguments) {
  print('Main: start.');
  _build();
  print('Main: end.');
}

// Draw some stuff, make some decisions
void _build() {
  print('build: Before getName.');
  _getName();
  print('build: After getName.  Name is: $name');
}

// Get some data, make some decisions
Future<void> _getName() async {
  print('getName: before getData');
  String data = await _getData();
  print('getName: after getData');
  double val = Random().nextDouble();
  if (val < 0.5) {
    print('getName: setting name = body.data');
    name = data;
  } else {
    print('getName: setting name = Bob');
    name = 'Bob';
  }
  return;
}

// Get the data via an http request
Future<String> _getData() async {
  print('getData: Before http get.');
  final data = await http.get(Uri.parse('http://www.google.co.uk'));
  print('getData: After http get.');
  return data.body;
}

The output from this code is shown below. Note that the build() method completed before _getData and _getName and the name printed in build() is empty in the 5th row.

Main: start.
build: Before getName.   
getName: before getData  
getData: Before http get.
build: After getName.  Name is: 
Main: end.
getData: After http get.
getName: after getData
getName: setting name = body.data

In the third example below, I have tried using .then to ensure that the code in each function only executes after the await command. I didn't think this would work (and it didn't) because I think I have a problem controlling the flow between functions, not a problem controlling the flow within functions, but I thought I should give it a go and I was clutching at straws by this point.

// Use global variable to avoid using await in build() method
// Use .then to ensure that method actions follow await command

import 'dart:async';
import 'dart:math';
import 'package:http/http.dart' as http;

String name = ""; // Global variable for async data return

void main(List<String> arguments) {
  print('Main: start.');
  _build();
  print('Main: end.');
}

// Draw some stuff, make some decisions
void _build() {
  print('build: Before getName.');
  _getName();
  print('build: After getName.  Name is: $name');
}

// Get some data, make some decisions
Future<void> _getName() async {
  print('getName: before getData');
  await _getData().then((data) {
    print('getName: after getData');
    double val = Random().nextDouble();
    if (val < 0.5) {
      print('getName: setting name = body.data');
      name = data;
    } else {
      print('getName: setting name = Bob');
      name = 'Bob';
    }
  });
  return;
}

// Get the data via an http request
Future<String> _getData() async {
  print('getData: Before http get.');
  String value = "";
  await http.get(Uri.parse('http://www.google.co.uk')).then((data) {
    print('getData: After http get.');
    value = data.body;
  });
  return value;
}

The output from this code is shown below. As with the second example, the execution is not in the correct order and the name printed in the build() method is empty.

Main: start.
build: Before getName.   
getName: before getData  
getData: Before http get.
build: After getName.  Name is: 
Main: end.
getData: After http get.
getName: after getData     
getName: setting name = Bob

Ideally, the output from the programme should be:

Main: start.
build: Before getName.   
getName: before getData  
getData: Before http get.
getData: After http get.
getName: after getData     
getName: setting name = Bob
build: After getName.  Name is: Bob 
Main: end.

How do I write my code so that I can use the http.get method and ensure that my code executes in the order that I want? I'll just add that I have read A LOT of stackoverflow questions, flutter documentation and general help online, but not found anything that answers my question so far. Or nothing that I understand. :D Apologies if this is a stupid question. I am a noob at this.

I should have added that this example is an simplification of the problem in a Flutter app I am writing (noughts and crosses). This is checking for a win/draw after each move, then reading data from a DB, updating the results and writing them back to the DB. It also updates the game state to show that the game is over. The problem caused by async/await is that gamestate isn't being updated whilst the functions await the data and the game continues in the "playing" state even though the game is over. Pseudo code of the programme below (this is a bit scrappy, but hopefully it illustrates the problem).

build() {
  checkState(win, draw or continue?);
    if (continue){
      _humanMove();
      _computerMove();
      drawTheWidgets;
    } else {
      drawDifferentWidgets; // New game
    }
}

void _humanMove() async {
    processMove;
    if (win/draw) { 
      await _updateResults; 
      updateGameState(game over);
    }
}

void _computerMove() async {
    processMove;
    if (win/draw) { 
      await _updateResults; 
      updateGameState(game over);
    }
}

results _updateResults() async {
  await http.get data results in database;
  updateWithResult;
  await http.put data results in database;
}

Answer

Question answered by Stephan (source).

You need to wait ("await") for all the function calls, see:

void main(List<String> arguments) async {
  print('Main: start.');
  await _build();
  print('Main: end.');
}

// Draw some stuff, make some decisions
Future<void> _build() async {
  print('build: Before getName.');
  await _getName();
  print('build: After getName.  Name is: $name');
}

// Get some data, make some decisions
Future<void> _getName() async {
  print('getName: before getData');
  String data = await Future.delayed(const Duration(milliseconds: 500), () {
    return 'x';
  });
  print('getName: after getData');
  double val = 5;
  if (val < 0.5) {
    print('getName: setting name = body.data');
    name = data;
  } else {
    print('getName: setting name = Bob');
    name = 'Bob';
  }
  return;
}

One comment: I believe it's easier for you to learn to do this right, if you try to learn this not based on console output, but with a Flutter app. You would see that your program goes through different states that actually need time - before, during and after your http request. In your build method of a widget, you would need to provide something to show for for each state of your program. So you actually do not wait (await) for results, but update the state based on the results. And depending on that, your build method is prepared to take the different states and show something adequate. Then async/await is quite nice.

--- adding the following in response to the clarifying comment ---

The build method of a widget is not where you put business logic of your app. Build is called whenever a widget is rebuild (if you have a http request writing in a database, it would be triggered a whole number of times!). Trying to apply the steps you wanted your app to do in your first description with the intended output on the console, I tried to write this clarifying app (and to have different states that your app passes through I included that you have to click the icon button / FloatingActionButton to trigger the process). Please have a look how the build method of the second widget deals with all the different states (it prints them to the console and shows a text - you could do more fancy stuff there of course, based on what the state of your app implies). The real "action" (changes of the state of your app) is happening elsewhere:

import 'package:flutter/material.dart';

class ExampleWidget extends StatefulWidget {
  const ExampleWidget({Key? key}) : super(key: key);

  @override
  _ExampleWidgetState createState() => _ExampleWidgetState();
}

class _ExampleWidgetState extends State<ExampleWidget> {
  String _showWhatsHappening = 'Before getName.';

  Future<String> _getData() async {
    setState(() {
      _showWhatsHappening = 'getData: Before http get.';
    });
    final data = await Future.delayed(const Duration(milliseconds: 500), () {
      return 'Bob';
    });
    setState(() {
      _showWhatsHappening = 'getData: After http get.';
    });
    await Future.delayed(const Duration(milliseconds: 300));
    return data;
  }

  @override
  void initState() {
    print('not main, but initstate: start.');
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Demo',
      theme: ThemeData(brightness: Brightness.light, fontFamily: 'Example'),
      home: Scaffold(
        floatingActionButton: FloatingActionButton(
          child: const Icon(Icons.star_outline),
          onPressed: () async {
            setState(() {
              _showWhatsHappening = 'before getData  ';
            });
            final x = await _getData();
            await Future.delayed(const Duration(milliseconds: 300));
            setState(() {
              _showWhatsHappening = 'after getData  ';
            });
            await Future.delayed(const Duration(milliseconds: 300));

            setState(() {
              _showWhatsHappening = 'setting name = $x';
            });
          },
        ),
        body: Center(child: ShowItWidget(showIt: _showWhatsHappening)),
      ),
    );
  }
}

class ShowItWidget extends StatelessWidget {
  final String showIt;
  const ShowItWidget({Key? key, required this.showIt}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    print(showIt);
    return Text(showIt);
  }
}

void main() {
  runApp(const ExampleWidget());
}
ASYNC-AWAIT ASYNCHRONOUS DART FLUTTER
SHARE: