Asynchronous code

class _ChooseLocationState extends State<ChooseLocation> {

int counter = 0;
void getData() async { // using async function

    // simulate network request for a username
    String username = await Future.delayed(Duration(seconds: 3), () { // use await keyword to wait till finish
    return 'sso';
    });

    // simulate network request to get bio of the username
    String bio = await Future.delayed(Duration(seconds: 2), () { // bio depends on username --> use await to do it
    return 'Afreeca BJ and youtuber';
    });

    print('$username - $bio');
}

@override
void initState() { // fires once when we load it
    super.initState();
    getData();
    print('initState function ran');
}



@override
Widget build(BuildContext context) { // fires every time we build
    print('build function ran');
    return Scaffold(
    backgroundColor: Colors.grey[200],
    appBar: AppBar(
        backgroundColor: Colors.blue[900],
        title: Text('Choose location'),
        centerTitle: true,
        elevation: 0,
    ),
    body: RaisedButton(
        onPressed: (){
        setState(() { // trigger build widget to update data on screen
            counter += 1;
        });
        },
        child: Text('counter is $counter'),
    )
    );
}
}