CINXE.COM
doWhile method - Future class - dart:async library - Dart API
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1, user-scalable=no"> <meta name="description" content="API docs for the doWhile method from the Future class, for the Dart programming language."> <title>doWhile method - Future class - dart:async library - Dart API</title> <link rel="canonical" href="https://api.dart.dev/dart-async/Future/doWhile.html"> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:ital,wght@0,300;0,400;0,500;0,700;1,400&display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@24,400,0,0" rel="stylesheet"> <link rel="stylesheet" href="../../static-assets/github.css?v1"> <link rel="stylesheet" href="../../static-assets/styles.css?v1"> <link rel="icon" href="../../static-assets/favicon.png?v1"> <!-- Google tag (gtag.js) --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-VVQ8908SJ5"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-VVQ8908SJ5'); </script> <link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link rel="preload" href="https://fonts.googleapis.com/css2?family=Google+Sans+Text:wght@400&family=Google+Sans:wght@500&display=swap" as="style"> <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Google+Sans+Text:wght@400&family=Google+Sans:wght@500&display=swap"> <link rel="stylesheet" href="https://www.gstatic.com/glue/cookienotificationbar/cookienotificationbar.min.css"> </head> <body data-base-href="../../" data-using-base-href="false" class="light-theme"> <div id="overlay-under-drawer"></div> <header id="title"> <span id="sidenav-left-toggle" class="material-symbols-outlined" role="button" tabindex="0">menu</span> <ol class="breadcrumbs gt-separated dark hidden-xs"> <li><a href="../../index.html">Dart</a></li> <li><a href="../../dart-async/dart-async-library.html">dart:async</a></li> <li><a href="../../dart-async/Future-class.html">Future<span class="signature"><<wbr><span class="type-parameter">T</span>></span></a></li> <li class="self-crumb">doWhile static method</li> </ol> <div class="self-name">doWhile</div> <form class="search navbar-right" role="search"> <input type="text" id="search-box" autocomplete="off" disabled class="form-control typeahead" placeholder="Loading search..."> </form> <div class="toggle" id="theme-button" title="Toggle brightness"> <label for="theme"> <input type="checkbox" id="theme" value="light-theme"> <span id="dark-theme-button" class="material-symbols-outlined"> dark_mode </span> <span id="light-theme-button" class="material-symbols-outlined"> light_mode </span> </label> </div> </header> <main> <div id="dartdoc-main-content" class="main-content" data-above-sidebar="dart-async/Future-class-sidebar.html" data-below-sidebar=""> <div> <div id="external-links" class="btn-group"><a title="View source code" class="source-link" href="https://github.com/dart-lang/sdk/blob/88c9758ef131d430d8ce595c6bfb4c90574d3ddd/sdk/lib/async/future.dart#L709"><span class="material-symbols-outlined">description</span></a></div> <h1><span class="kind-method">doWhile</span> static method </h1></div> <section class="multi-line-signature"> <span class="returntype"><a href="../../dart-async/Future-class.html">Future</a><span class="signature"><<wbr><span class="type-parameter">void</span>></span></span> <span class="name ">doWhile</span>(<wbr><ol class="parameter-list single-line"> <li><span class="parameter" id="doWhile-param-action"><span class="type-annotation"><a href="../../dart-async/FutureOr-class.html">FutureOr</a><span class="signature"><<wbr><span class="type-parameter"><a href="../../dart-core/bool-class.html">bool</a></span>></span></span> <span class="parameter-name">action</span>()</span></li> </ol>) </section> <section class="desc markdown"> <p>Performs an operation repeatedly until it returns <code>false</code>.</p> <p>The operation, <code>action</code>, may be either synchronous or asynchronous.</p> <p>The operation is called repeatedly as long as it returns either the <a href="../../dart-core/bool-class.html">bool</a> value <code>true</code> or a <code>Future<bool></code> which completes with the value <code>true</code>.</p> <p>If a call to <code>action</code> returns <code>false</code> or a <a href="../../dart-async/Future-class.html">Future</a> that completes to <code>false</code>, iteration ends and the future returned by <a href="../../dart-async/Future/doWhile.html">doWhile</a> is completed with a <code>null</code> value.</p> <p>If a call to <code>action</code> throws or a future returned by <code>action</code> completes with an error, iteration ends and the future returned by <a href="../../dart-async/Future/doWhile.html">doWhile</a> completes with the same error.</p> <p>Calls to <code>action</code> may happen at any time, including immediately after calling <code>doWhile</code>. The only restriction is a new call to <code>action</code> won't happen before the previous call has returned, and if it returned a <code>Future<bool></code>, not until that future has completed.</p> <p>Example:</p> <pre class="language-dart"><code class="language-dart">void main() async { var value = 0; await Future.doWhile(() async { value++; await Future.delayed(const Duration(seconds: 1)); if (value == 3) { print('Finished with $value'); return false; } return true; }); } // Outputs: 'Finished with 3' </code></pre> </section> <section class="summary source-code" id="source"> <h2><span>Implementation</span></h2> <pre class="language-dart"><code class="language-dart">static Future<void> doWhile(FutureOr<bool> action()) { _Future<void> doneSignal = new _Future<void>(); late void Function(bool) nextIteration; // Bind this callback explicitly so that each iteration isn't bound in the // context of all the previous iterations' callbacks. // This avoids, e.g., deeply nested stack traces from the stack trace // package. nextIteration = Zone.current.bindUnaryCallbackGuarded((bool keepGoing) { while (keepGoing) { FutureOr<bool> result; try { result = action(); } catch (error, stackTrace) { // Cannot use _completeWithErrorCallback because it completes // the future synchronously. _asyncCompleteWithErrorCallback(doneSignal, error, stackTrace); return; } if (result is Future<bool>) { result.then(nextIteration, onError: doneSignal._completeError); return; } keepGoing = result; } doneSignal._complete(null); }); nextIteration(true); return doneSignal; }</code></pre> </section> </div> <!-- /.main-content --> <div id="dartdoc-sidebar-left" class="sidebar sidebar-offcanvas-left"> <!-- The search input and breadcrumbs below are only responsively visible at low resolutions. --> <header id="header-search-sidebar" class="hidden-l"> <form class="search-sidebar" role="search"> <input type="text" id="search-sidebar" autocomplete="off" disabled class="form-control typeahead" placeholder="Loading search..."> </form> </header> <ol class="breadcrumbs gt-separated dark hidden-l" id="sidebar-nav"> <li><a href="../../index.html">Dart</a></li> <li><a href="../../dart-async/dart-async-library.html">dart:async</a></li> <li><a href="../../dart-async/Future-class.html">Future<span class="signature"><<wbr><span class="type-parameter">T</span>></span></a></li> <li class="self-crumb">doWhile static method</li> </ol> <h5>Future class</h5> <div id="dartdoc-sidebar-left-content"></div> </div><!--/.sidebar-offcanvas--> <div id="dartdoc-sidebar-right" class="sidebar sidebar-offcanvas-right"> </div><!--/.sidebar-offcanvas--> </main> <footer> <span class="no-break"> Dart 3.5.4 </span> <span class="glue-footer"> <span class="no-break"> | <a href="https://dart.dev/terms" title="Terms of use">Terms</a> </span> <span class="no-break"> | <a href="https://policies.google.com/privacy" target="_blank" rel="noopener" title="Privacy policy" class="no-automatic-external">Privacy</a> </span> <span class="no-break"> | <a href="https://dart.dev/security" title="Security philosophy and practices">Security</a> </span> <div class="copyright" style="font-size: 0.9em; color: darkgrey; margin-top: 0.5em;"> Except as otherwise noted, this site is licensed under a <a style="color: darkgrey;" href="https://creativecommons.org/licenses/by/4.0/"> Creative Commons Attribution 4.0 International License</a> and code samples are licensed under the <a style="color: darkgrey;" href="https://opensource.org/licenses/BSD-3-Clause" class="no-automatic-external"> 3-Clause BSD License</a> </div> </span> </footer> <script src="../../static-assets/highlight.pack.js?v1"></script> <script src="../../static-assets/docs.dart.js"></script> <button aria-hidden="true" class="glue-footer__link glue-cookie-notification-bar-control"> Cookies management controls </button> <script src="https://www.gstatic.com/glue/cookienotificationbar/cookienotificationbar.min.js" data-glue-cookie-notification-bar-category="2B"> </script> </body> </html>