waitForTrue method

Future<bool> waitForTrue(
  1. bool checkFunction(), {
  2. Duration checkInterval = const Duration(milliseconds: 300),
})

Implementation

Future<bool> waitForTrue(
  bool Function() checkFunction, {
  Duration checkInterval =
      const Duration(milliseconds: 300), // Interval between checks
}) async {
  final Duration timeout = Duration(milliseconds: toInt());
  final stopwatch = Stopwatch()..start(); // Start a stopwatch to track time
  while (stopwatch.elapsed < timeout) {
    try {
      if (checkFunction()) {
        // Await the result of the checkFunction
        return true; // Function returned true, we're done!
      }
    } catch (e) {
      // You might want to return false or re-throw the error depending on your needs
      return false; // Or throw e;
    }

    await Future.delayed(
        checkInterval); // Wait for the interval before checking again
  }

  stopwatch.stop(); // Stop the stopwatch

  return false; // Timeout reached, function never returned true within the time limit
}