Flutter: A few ways to simulate slow-responding functions and methods

As a quick note, here are a couple of examples of how to simulate a slow-responding Flutter method:

Future<bool> _getFutureBool() {
    return Future.delayed(Duration(milliseconds: 500))
                 .then((onValue) => true);
}

static Future<bool> getEnableNotifications() async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    return Future.delayed(const Duration(milliseconds: 1000), () {
        return prefs.getBool(KEY_ENABLE_NOTIFICATIONS) ?? false;      
    });
}

As shown, the first example uses Future.delayed followed by a then call, and the second example shows Future.delayed with a lambda function.

You’ll want to use this code/technique when you’re working with Flutter futures and its FutureBuilder class.

I was reminded about the need to test things likes futures and FutureBuilder when some code I found on the internet wasn’t working properly. As a result I wrote my tutorial, How to correctly create a Flutter FutureBuilder