CINXE.COM

dartx | Dart package

<!DOCTYPE html> <html lang="en-us"><head><script src="https://www.googletagmanager.com/gtm.js?id=GTM-MX6DBN9" async="async"></script><script src="/static/hash-pb35dgsm/js/gtm.js" async="async"></script><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="ie=edge"/><meta name="viewport" content="width=device-width, initial-scale=1"/><meta name="twitter:card" content="summary"/><meta name="twitter:site" content="@dart_lang"/><meta name="twitter:description" content="Superpowers for Dart. Collection of useful static extension methods."/><meta name="twitter:image" content="https://pub.dev/static/hash-pb35dgsm/img/pub-dev-icon-cover-image.png"/><meta property="og:type" content="website"/><meta property="og:site_name" content="Dart packages"/><meta property="og:title" content="dartx | Dart package"/><meta property="og:description" content="Superpowers for Dart. Collection of useful static extension methods."/><meta property="og:image" content="https://pub.dev/static/hash-pb35dgsm/img/pub-dev-icon-cover-image.png"/><meta property="og:url" content="https://pub.dev/packages/dartx"/><title>dartx | Dart package</title><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Google+Sans:wght@400;500;700&amp;family=Google+Sans+Display:wght@400&amp;family=Google+Sans+Text:wght@400;500;700&amp;family=Google+Sans+Mono:wght@400;700&amp;display=swap"/><link rel="shortcut icon" href="/favicon.ico?hash=nk4nss8c7444fg0chird9erqef2vkhb8"/><meta rel="apple-touch-icon" href="/static/hash-pb35dgsm/img/apple-touch-icon.png"/><meta rel="apple-touch-icon-precomposed" href="/static/hash-pb35dgsm/img/apple-touch-icon.png"/><link rel="stylesheet" href="https://www.gstatic.com/glue/v25_0/ccb.min.css"/><link rel="search" type="application/opensearchdescription+xml" title="Dart packages" href="/osd.xml"/><link rel="canonical" href="https://pub.dev/packages/dartx"/><meta name="description" content="Superpowers for Dart. Collection of useful static extension methods."/><link rel="alternate" type="application/atom+xml" title="Updated Packages Feed for Pub" href="/feed.atom"/><link rel="stylesheet" type="text/css" href="/static/hash-pb35dgsm/material/bundle/styles.css"/><link rel="stylesheet" type="text/css" href="/static/hash-pb35dgsm/css/style.css"/><script src="/static/hash-pb35dgsm/material/bundle/script.min.js" defer="defer"></script><script src="/static/hash-pb35dgsm/js/script.dart.js" defer="defer"></script><script src="https://www.gstatic.com/brandstudio/kato/cookie_choice_component/cookie_consent_bar.v3.js" defer="defer" data-autoload-cookie-consent-bar="true"></script><meta name="pub-page-data" content="eyJwa2dEYXRhIjp7InBhY2thZ2UiOiJkYXJ0eCIsInZlcnNpb24iOiIxLjIuMCIsImxpa2VzIjo5NDgsInB1Ymxpc2hlcklkIjoic2ltYy5kZXYiLCJpc0Rpc2NvbnRpbnVlZCI6ZmFsc2UsImlzTGF0ZXN0Ijp0cnVlfSwic2Vzc2lvbkF3YXJlIjpmYWxzZX0="/><link rel="preload" href="/static/hash-pb35dgsm/highlight/highlight-with-init.js" as="script"/></head><body class="light-theme"><script src="/static/hash-pb35dgsm/js/dark-init.js"></script><noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-MX6DBN9" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript><div class="site-header"><button class="hamburger" aria-label="menu toggle"></button><a class="logo" href="/" aria-label="Go to the landing page of pub.dev"><img class="site-logo" src="/static/hash-pb35dgsm/img/pub-dev-logo.svg" alt="" width="140" height="30" role="presentation"/></a><div class="site-header-space"></div><div class="site-header-mask"></div><div class="site-header-search"><form action="/packages" method="GET"><input class="site-header-search-input" name="q" placeholder="New search..." autocomplete="on" title="Search"/></form></div><nav class="site-header-nav scroll-container"><div class="nav-login-container"><button id="-account-login" class="nav-main-button link">Sign in</button></div><div class="nav-container nav-help-container hoverable"><button class="nav-main-button">Help</button><div class="nav-hover-popup"><div class="nav-table-columns"><div class="nav-table-column"><h3>pub.dev</h3><a class="nav-link" href="/help/search" rel="noopener" target="_blank">Searching for packages</a><a class="nav-link" href="/help/scoring" rel="noopener" target="_blank">Package scoring and pub points</a></div><div class="nav-table-column"><h3>Flutter</h3><a class="nav-link" href="https://flutter.dev/using-packages/" rel="noopener" target="_blank">Using packages</a><a class="nav-link" href="https://flutter.dev/developing-packages/" rel="noopener" target="_blank">Developing packages and plugins</a><a class="nav-link" href="https://dart.dev/tools/pub/publishing" rel="noopener" target="_blank">Publishing a package</a></div><div class="nav-table-column"><h3>Dart</h3><a class="nav-link" href="https://dart.dev/guides/packages" rel="noopener" target="_blank">Using packages</a><a class="nav-link" href="https://dart.dev/tools/pub/publishing" rel="noopener" target="_blank">Publishing a package</a></div></div></div></div><div class="nav-container nav-help-container-mobile foldable"><h3 class="foldable-button">pub.dev <img class="foldable-icon" src="/static/hash-pb35dgsm/img/nav-mobile-foldable-icon.svg" alt="toggle folding of the section" width="13" height="6"/></h3><div class="foldable-content"><a class="nav-link" href="/help/search" rel="noopener" target="_blank">Searching for packages</a><a class="nav-link" href="/help/scoring" rel="noopener" target="_blank">Package scoring and pub points</a></div></div><div class="nav-container nav-help-container-mobile foldable"><h3 class="foldable-button">Flutter <img class="foldable-icon" src="/static/hash-pb35dgsm/img/nav-mobile-foldable-icon.svg" alt="toggle folding of the section" width="13" height="6"/></h3><div class="foldable-content"><a class="nav-link" href="https://flutter.dev/using-packages/" rel="noopener" target="_blank">Using packages</a><a class="nav-link" href="https://flutter.dev/developing-packages/" rel="noopener" target="_blank">Developing packages and plugins</a><a class="nav-link" href="https://dart.dev/tools/pub/publishing" rel="noopener" target="_blank">Publishing a package</a></div></div><div class="nav-container nav-help-container-mobile foldable"><h3 class="foldable-button">Dart <img class="foldable-icon" src="/static/hash-pb35dgsm/img/nav-mobile-foldable-icon.svg" alt="toggle folding of the section" width="13" height="6"/></h3><div class="foldable-content"><a class="nav-link" href="https://dart.dev/guides/packages" rel="noopener" target="_blank">Using packages</a><a class="nav-link" href="https://dart.dev/tools/pub/publishing" rel="noopener" target="_blank">Publishing a package</a></div></div></nav><button class="-pub-theme-toggle" aria-label="light/dark theme toggle"></button></div><div id="banner-container"></div><main class="container"><div class="detail-wrapper -active -has-info-box"><div class="detail-header -is-loose"><div class="detail-container"><div class="detail-header-outer-block"><div class="detail-header-content-block"><h1 class="title pub-monochrome-icon-hoverable">dartx 1.2.0 <span class="pkg-page-title-copy"><img class="pub-monochrome-icon pkg-page-title-copy-icon filter-invert-on-dark" src="/static/hash-pb35dgsm/img/content-copy-icon.svg" alt="copy &quot;dartx: ^1.2.0&quot; to clipboard" width="18" height="18" title="Copy &quot;dartx: ^1.2.0&quot; to clipboard" data-copy-content="dartx: ^1.2.0" data-ga-click-event="copy-package-version"/><div class="pkg-page-title-copy-feedback"><span class="code">dartx: ^1.2.0</span> copied to clipboard</div></span></h1><div class="metadata">Published <span><a class="-x-ago" href="" title="Jul 18, 2023" role="button" data-timestamp="1689682928941">20 months ago</a></span> • <a class="-pub-publisher" href="/publishers/simc.dev"><img class="-pub-publisher-shield filter-invert-on-dark" src="/static/hash-pb35dgsm/img/material-icon-verified.svg" alt="verified publisher" width="14" height="14" title="Published by a pub.dev verified publisher"/>simc.dev</a><span class="package-badge" title="Package is compatible with Dart 3.">Dart 3 compatible</span></div><div class="detail-tags-and-like"><div class="detail-tags"><div class="-pub-tag-badge"><span class="tag-badge-main">SDK</span><a class="tag-badge-sub" href="/packages?q=sdk%3Adart" rel="nofollow" title="Packages compatible with Dart SDK">Dart</a><a class="tag-badge-sub" href="/packages?q=sdk%3Aflutter" rel="nofollow" title="Packages compatible with Flutter SDK">Flutter</a></div><div class="-pub-tag-badge"><span class="tag-badge-main">Platform</span><a class="tag-badge-sub" href="/packages?q=platform%3Aandroid" rel="nofollow" title="Packages compatible with Android platform">Android</a><a class="tag-badge-sub" href="/packages?q=platform%3Aios" rel="nofollow" title="Packages compatible with iOS platform">iOS</a><a class="tag-badge-sub" href="/packages?q=platform%3Alinux" rel="nofollow" title="Packages compatible with Linux platform">Linux</a><a class="tag-badge-sub" href="/packages?q=platform%3Amacos" rel="nofollow" title="Packages compatible with macOS platform">macOS</a><a class="tag-badge-sub" href="/packages?q=platform%3Aweb" rel="nofollow" title="Packages compatible with Web platform">web</a><a class="tag-badge-sub" href="/packages?q=platform%3Awindows" rel="nofollow" title="Packages compatible with Windows platform">Windows</a></div></div><div class="detail-like"><button id="-pub-like-icon-button" class="mdc-icon-button" data-ga-click-event="toggle-like" aria-pressed="false" title="Like this package"><img class="mdc-icon-button__icon" src="/static/hash-pb35dgsm/img/like-inactive.svg" alt="liked status: inactive" width="18" height="18"/><img class="mdc-icon-button__icon mdc-icon-button__icon--on" src="/static/hash-pb35dgsm/img/like-active.svg" alt="liked status: active" width="18" height="18"/></button><span class="likes-count"><span id="likes-count">948</span></span></div></div></div></div></div></div><div class="detail-container"><div class="detail-lead"><div class="detail-metadata-toggle"><div class="detail-metadata-toggle-icon">→</div><h3 class="detail-lead-title">Metadata</h3></div><p class="detail-lead-text">Superpowers for Dart. Collection of useful static extension methods.</p><p class="detail-lead-more"><a class="detail-metadata-toggle">More...</a></p></div></div><div class="detail-body"><div class="detail-tabs"><div class="detail-tabs-wide-header"><div class="detail-container"><ul class="detail-tabs-header"><li class="detail-tab tab-button detail-tab-readme-title -active">Readme</li><li class="detail-tab tab-link detail-tab-changelog-title"><a href="/packages/dartx/changelog" role="button">Changelog</a></li><li class="detail-tab tab-link detail-tab-example-title"><a href="/packages/dartx/example" role="button">Example</a></li><li class="detail-tab tab-link detail-tab-installing-title"><a href="/packages/dartx/install" role="button">Installing</a></li><li class="detail-tab tab-link detail-tab-versions-title"><a href="/packages/dartx/versions" role="button">Versions</a></li><li class="detail-tab tab-link detail-tab-analysis-title"><a href="/packages/dartx/score" role="button">Scores</a></li></ul></div></div><div class="detail-container detail-body-main"><div class="detail-tabs-content"><section class="tab-content detail-tab-readme-content -active markdown-body"><img src="https://raw.githubusercontent.com/leisim/dartx/master/.github/logo.svg?sanitize=true" width="500px"> <p><a href="https://github.com/leisim/dartx/actions" rel="ugc"><img src="https://github.com/leisim/dartx/workflows/Dart%20CI/badge.svg?branch=master" alt="Dart CI"></a> <a href="https://codecov.io/gh/leisim/dartx" rel="ugc"><img src="https://img.shields.io/codecov/c/github/leisim/dartx.svg" alt="Codecov"></a> <a href="https://pub.dev/packages/dartx"><img src="https://img.shields.io/pub/v/dartx?label=dartx" alt="dartx"></a> <a href="https://pub.dev/packages/flutterx"><img src="https://img.shields.io/pub/v/flutterx?label=flutterx" alt="flutterx"></a></p> <p><em>If you miss an extension, please open an issue or pull request</em></p> <h3 id="resources" class="hash-header">Resources: <a href="#resources" class="hash-link">#</a></h3> <ul> <li><a href="https://pub.dev/documentation/dartx/latest/dartx/dartx-library.html">Documentation</a></li> <li><a href="https://pub.dev/packages/dartx">Pub Package</a></li> <li><a href="https://github.com/leisim/dartx" rel="ugc">GitHub Repository</a></li> </ul> <p>On this page you can find some of the extensions. Take a look at the docs to see all of them.</p> <h2 id="getting-started-" class="hash-header">Getting started 🎉 <a href="#getting-started-" class="hash-link">#</a></h2> <p>Add the following to your <code>pubspec.yaml</code>:</p> <pre><code class="language-dart">dependencies: dartx: any </code></pre> <p>After you import the library, you can use the extensions.</p> <pre><code class="language-dart">import 'package:dartx/dartx.dart'; final slice = [1, 2, 3, 4, 5].slice(1, -2); // [2, 3, 4] </code></pre> <h2 id="iterable" class="hash-header">Iterable <a href="#iterable" class="hash-link">#</a></h2> <h3 id="slice" class="hash-header">.slice() <a href="#slice" class="hash-link">#</a></h3> <p>Returns elements at indices between <code>start</code> (inclusive) and <code>end</code> (inclusive).</p> <pre><code class="language-dart">final list = [0, 1, 2, 3, 4, 5]; final last = list.slice(-1); // [5] final lastHalf = list.slice(3); // [3, 4, 5] final allButFirstAndLast = list.slice(1, -2); // [1, 2, 3, 4] </code></pre> <h3 id="sortedby--thenby" class="hash-header">.sortedBy() &amp; .thenBy() <a href="#sortedby--thenby" class="hash-link">#</a></h3> <p>Sort lists by multiple properties.</p> <pre><code class="language-dart">final dogs = [ Dog(name: "Tom", age: 3), Dog(name: "Charlie", age: 7), Dog(name: "Bark", age: 1), Dog(name: "Cookie", age: 4), Dog(name: "Charlie", age: 2), ]; final sorted = dogs .sortedBy((dog) =&gt; dog.name) .thenByDescending((dog) =&gt; dog.age); // Bark, Cookie, Charlie (7), Charlie (2), Tom </code></pre> <h3 id="distinctby" class="hash-header">.distinctBy() <a href="#distinctby" class="hash-link">#</a></h3> <p>Get distinct elements from a list.</p> <pre><code class="language-dart">final list = ['this', 'is', 'a', 'test']; final distinctByLength = list.distinctBy((it) =&gt; it.length); // ['this', 'is', 'a'] </code></pre> <h3 id="flatten" class="hash-header">.flatten() <a href="#flatten" class="hash-link">#</a></h3> <p>Get a new lazy <code>Iterable</code> of all elements from all collections in a collection.</p> <pre><code class="language-dart">final nestedList = [[1, 2, 3], [4, 5, 6]]; final flattened = nestedList.flatten(); // [1, 2, 3, 4, 5, 6] </code></pre> <h3 id="chunkwhile--splitwhen" class="hash-header">.chunkWhile() &amp; .splitWhen() <a href="#chunkwhile--splitwhen" class="hash-link">#</a></h3> <p>Chunk entries as long as two elements match a predicate:</p> <pre><code class="language-dart">final list = [1, 2, 4, 9, 10, 11, 12, 15, 16, 19, 20, 21]; final increasingSubSequences = list.chunkWhile((a, b) =&gt; a + 1 == b); // increasingSubSequences = [[1, 2], [4], [9, 10, 11, 12], [15, 16], [19, 20, 21]] </code></pre> <p><code>splitWhen</code> is the opposite of <code>chunkWhile</code> that starts a new chunk every time the predicate <em>didn't</em> match.</p> <h2 id="int" class="hash-header">int <a href="#int" class="hash-link">#</a></h2> <h3 id="buildstring" class="hash-header">buildString() <a href="#buildstring" class="hash-link">#</a></h3> <p>Builds new string by populating newly created <code>StringBuffer</code> using provided <code>builderAction</code> and then converting it to <code>String</code>.</p> <pre><code class="language-dart">final word = buildString((sb) { for (var i = 0; i &lt; 10; i++) { sb.write(i); } }); // 0123456789 </code></pre> <h3 id="ordinal" class="hash-header">.ordinal <a href="#ordinal" class="hash-link">#</a></h3> <p>Returns an ordinal number of <code>String</code> type for any integer</p> <pre><code class="language-dart">final a = 1.ordinal(); // 1st final b = 108.ordinal(); // 108th </code></pre> <h2 id="string" class="hash-header">String <a href="#string" class="hash-link">#</a></h2> <h3 id="capitalize" class="hash-header">.capitalize <a href="#capitalize" class="hash-link">#</a></h3> <p>Returns a copy of the string having its first letter uppercased, or the original string, if it's empty or already starts with an upper case letter.</p> <pre><code class="language-dart">final word = 'abcd'.capitalize(); // Abcd final anotherWord = 'Abcd'.capitalize(); // Abcd </code></pre> <h3 id="decapitalize" class="hash-header">.decapitalize <a href="#decapitalize" class="hash-link">#</a></h3> <p>Returns a copy of the string having its first letter lowercased, or the original string, if it's empty or already starts with a lower case letter.</p> <pre><code class="language-dart">final word = 'abcd'.decapitalize(); // abcd final anotherWord = 'Abcd'.decapitalize(); // abcd </code></pre> <h3 id="isascii" class="hash-header">.isAscii <a href="#isascii" class="hash-link">#</a></h3> <p>Returns <code>true</code> if the string is ASCII encoded.</p> <pre><code class="language-dart">final isAscii = 'abc123 !,.~'.isAscii; // true final isNotAscii = '§3'.isAscii; // false </code></pre> <h3 id="isblank" class="hash-header">.isBlank <a href="#isblank" class="hash-link">#</a></h3> <p>Returns <code>true</code> if this string is empty or consists solely of whitespace characters.</p> <pre><code class="language-dart">final notBlank = ' .'.isBlank; // false final blank = ' '.isBlank; // true </code></pre> <h3 id="isdouble" class="hash-header">.isDouble <a href="#isdouble" class="hash-link">#</a></h3> <p>Returns <code>true</code> if the string can be parsed as a double.</p> <pre><code class="language-dart">final a = ''.isDouble; // false final b = 'a'.isDouble; // false final c = '1'.isDouble; // true final d = '1.0'.isDouble; // true final e = '123456789.987654321'.isDouble; // true final f = '1,000'.isDouble; // false </code></pre> <h3 id="isint" class="hash-header">.isInt <a href="#isint" class="hash-link">#</a></h3> <p>Returns <code>true</code> if the string can be parsed as an integer.</p> <pre><code class="language-dart">final a = ''.isInt; // false final b = 'a'.isInt; // false final c = '1'.isInt; // true final d = '1.0'.isInt; // false final e = '1,000'.isInt; // false </code></pre> <h3 id="islatin1" class="hash-header">.isLatin1 <a href="#islatin1" class="hash-link">#</a></h3> <p>Returns <code>true</code> if the string is Latin 1 encoded.</p> <pre><code class="language-dart">final isLatin1 = '§Êü'.isLatin1; // true final isNotLatin1 = 'ő'.isLatin1; // false </code></pre> <h3 id="islowercase" class="hash-header">.isLowerCase <a href="#islowercase" class="hash-link">#</a></h3> <p>Returns <code>true</code> if the entire string is lower case.</p> <pre><code class="language-dart">final a = 'abc'.isLowerCase; // true final b = 'abC'.isLowerCase; // false final c = ' '.isLowerCase; // true final d = ''.isLowerCase; // false </code></pre> <h3 id="isnotblank" class="hash-header">.isNotBlank <a href="#isnotblank" class="hash-link">#</a></h3> <p>Returns <code>true</code> if this string is not empty and contains characters except whitespace characters.</p> <pre><code class="language-dart">final blank = ' '.isNotBlank; // false final notBlank = ' .'.isNotBlank; // true </code></pre> <h3 id="isnullorempty" class="hash-header">.isNullOrEmpty <a href="#isnullorempty" class="hash-link">#</a></h3> <p>Returns <code>true</code> if the String is either <code>null</code> or empty.</p> <pre><code class="language-dart">final isNull = null.isNullOrEmpty; // true final isEmpty = ''.isNullOrEmpty; // true final isBlank = ' '.isNullOrEmpty; // false final isLineBreak = '\n'.isNullOrEmpty; // false </code></pre> <h3 id="isnotnullorempty" class="hash-header">.isNotNullOrEmpty <a href="#isnotnullorempty" class="hash-link">#</a></h3> <p>Returns <code>true</code> if the String is neither <code>null</code> nor empty.</p> <pre><code class="language-dart">final isNull = null.isNullOrEmpty; // true final isEmpty = ''.isNullOrEmpty; // true final isBlank = ' '.isNullOrEmpty; // false final isLineBreak = '\n'.isNullOrEmpty; // false </code></pre> <h3 id="isnullorblank" class="hash-header">.isNullOrBlank <a href="#isnullorblank" class="hash-link">#</a></h3> <p>Returns <code>true</code> if the String is either <code>null</code> or blank.</p> <pre><code class="language-dart">final isNull = null.isNullOrBlank; // true final isEmpty = ''.isNullOrBlank; // true final isBlank = ' '.isNullOrBlank; // true final isLineBreak = '\n'.isNullOrBlank; // true final isFoo = ' foo '.isNullOrBlank; // false </code></pre> <h3 id="isnotnullorblank" class="hash-header">.isNotNullOrBlank <a href="#isnotnullorblank" class="hash-link">#</a></h3> <p>Returns <code>true</code> if the String is neither <code>null</code> nor blank.</p> <pre><code class="language-dart">final isNull = null.isNullOrBlank; // true final isEmpty = ''.isNullOrBlank; // true final isBlank = ' '.isNullOrBlank; // true final isLineBreak = '\n'.isNullOrBlank; // true final isFoo = ' foo '.isNullOrBlank; // true </code></pre> <h3 id="isuppercase" class="hash-header">.isUpperCase <a href="#isuppercase" class="hash-link">#</a></h3> <p>Returns <code>true</code> if the entire string is upper case.</p> <pre><code class="language-dart">final a = 'ABC'.isUpperCase; // true final b = 'ABc'.isUpperCase; // false final c = ' '.isUpperCase; // true final d = ''.isUpperCase; // false </code></pre> <h3 id="md5" class="hash-header">.md5 <a href="#md5" class="hash-link">#</a></h3> <p>Calculates the MD5 digest and returns the value as a string of hexadecimal digits.</p> <pre><code class="language-dart">final a = 'abc'.md5; // 900150983cd24fb0d6963f7d28e17f72 final b = 'ഐ⌛酪Б👨‍👨‍👧‍👦'.md5; // c7834eff7c967101cfb65b8f6d15ad46 </code></pre> <h3 id="urlencode" class="hash-header">.urlEncode <a href="#urlencode" class="hash-link">#</a></h3> <p>Translates a string into application/x-www-form-urlencoded format using a specific encoding scheme.</p> <pre><code class="language-dart">const originalUrl = 'Hello Ladies + Gentlemen, a signed OAuth request!'; final encodedUrl = originalUrl.urlEncode; // 'Hello%20Ladies%20+%20Gentlemen,%20a%20signed%20OAuth%20request!' </code></pre> <h3 id="urldecode" class="hash-header">.urlDecode <a href="#urldecode" class="hash-link">#</a></h3> <p>Decodes an application/x-www-form-urlencoded string using a specific encoding scheme.</p> <pre><code class="language-dart">const encodedUrl = 'Hello%20Ladies%20+%20Gentlemen,%20a%20signed%20OAuth%20request!'; final decodedUrl = encodingUrl.urlDecode; // 'Hello Ladies + Gentlemen, a signed OAuth request!' </code></pre> <h3 id="removeprefix-removesuffix-and-removesurrounding" class="hash-header">.removePrefix(), .removeSuffix() and .removeSurrounding() <a href="#removeprefix-removesuffix-and-removesurrounding" class="hash-link">#</a></h3> <p>Remove a prefix, a suffix, or both from a given string:</p> <pre><code class="language-dart">final name = 'James Bond'.removePrefix('James '); // Bond final milliseconds = '100ms'.removeSuffix('ms'); // 100 final text = '&lt;p&gt;Some HTML&lt;/p&gt;' .removeSurrounding(prefix: '&lt;p&gt;', suffix: '&lt;/p&gt;'); // Some HTML </code></pre> <h3 id="reversed" class="hash-header">.reversed <a href="#reversed" class="hash-link">#</a></h3> <p>Returns a new string with characters in reversed order.</p> <pre><code class="language-dart">final emptyString = ''.reversed; // '' final reversed = 'abc🤔'.reversed; // '🤔cba' </code></pre> <h3 id="slice-2" class="hash-header">.slice() <a href="#slice-2" class="hash-link">#</a></h3> <p>Returns a new substring containing all characters including indices [start] and [end]. If [end] is omitted, it is being set to <code>lastIndex</code>.</p> <pre><code class="language-dart">final sliceOne = 'awesomeString'.slice(0,6)); // awesome final sliceTwo = 'awesomeString'.slice(7)); // String </code></pre> <h3 id="todoubleornull" class="hash-header">.toDoubleOrNull() <a href="#todoubleornull" class="hash-link">#</a></h3> <p>Parses the string as a <code>double</code> and returns the result or <code>null</code> if the String is not a valid representation of a number.</p> <pre><code class="language-dart">final numOne = '1'.toDoubleOrNull(); // 1.0 final numTwo = '1.2'.toDoubleOrNull(); // 1.2 final blank = ''.toDoubleOrNull(); // null </code></pre> <h3 id="toint" class="hash-header">.toInt() <a href="#toint" class="hash-link">#</a></h3> <p>Parses the string as an integer and returns the result. The radix (base) thereby defaults to 10. Throws a <code>FormatException</code> if parsing fails.</p> <pre><code class="language-dart">final a = '1'.toInt(); // 1 final b = '100'.toInt(radix: 2); // 4 final c = '100'.toInt(radix: 16); // 256 final d = '1.0'.toInt(); // throws FormatException </code></pre> <h3 id="tointornull" class="hash-header">.toIntOrNull() <a href="#tointornull" class="hash-link">#</a></h3> <p>Parses the string as an integer or returns <code>null</code> if it is not a number.</p> <pre><code class="language-dart">final number = '12345'.toIntOrNull(); // 12345 final notANumber = '123-45'.toIntOrNull(); // null </code></pre> <h3 id="toutf8" class="hash-header">.toUtf8() <a href="#toutf8" class="hash-link">#</a></h3> <p>Converts String to UTF-8 encoding.</p> <pre><code class="language-dart">final emptyString = ''.toUtf8(); // [] final hi = 'hi'.toUtf8(); // [104, 105] final emoji = '😄'.toUtf8(); // [240, 159, 152, 132] </code></pre> <h3 id="toutf16" class="hash-header">.toUtf16() <a href="#toutf16" class="hash-link">#</a></h3> <p>Converts String to UTF-16 encoding.</p> <pre><code class="language-dart">final emptyString = ''.toUtf16(); // [] final hi = 'hi'.toUtf16(); // [104, 105] final emoji = '😄'.toUtf16(); // [55357, 56836] </code></pre> <h3 id="orempty" class="hash-header">.orEmpty() <a href="#orempty" class="hash-link">#</a></h3> <p>Returns the string if it is not <code>null</code>, or the empty string otherwise.</p> <pre><code class="language-dart">String? nullableStr; final str = nullableStr.orEmpty(); // '' </code></pre> <h3 id="matches" class="hash-header">.matches() <a href="#matches" class="hash-link">#</a></h3> <p>Returns <code>true</code> if this char sequence matches the given regular expression.</p> <pre><code class="language-dart">print('as'.matches(RegExp('^.s\$'))) // true print('mst'.matches(RegExp('^.s\$'))) // false </code></pre> <h3 id="time-utils" class="hash-header">Time utils <a href="#time-utils" class="hash-link">#</a></h3> <p>Dartx exports <a href="https://github.com/jogboms" rel="ugc">@jogboms</a> great <a href="https://github.com/jogboms/time.dart" rel="ugc">⏰ time.dart</a> package so you can do the following:</p> <pre><code class="language-dart">int secondsInADay = 1.days.inSeconds; Duration totalTime = [12.5.seconds, 101.milliseconds, 2.5.minutes].sum(); DateTime oneWeekLater = DateTime.now() + 1.week; </code></pre> <p>Check out <a href="https://github.com/jogboms/time.dart" rel="ugc">⏰ time.dart</a> for more information and examples.</p> <h2 id="num" class="hash-header">num <a href="#num" class="hash-link">#</a></h2> <h3 id="coercein" class="hash-header">.coerceIn() <a href="#coercein" class="hash-link">#</a></h3> <p>Ensures that this value lies in the specified range.</p> <pre><code class="language-dart">final numberInRange = 123.coerceIn(0, 1000); // 123 final numberOutOfRange = -123.coerceIn(0, 1000); // 0 </code></pre> <h3 id="tobytes" class="hash-header">.toBytes() <a href="#tobytes" class="hash-link">#</a></h3> <p>Converts this value to binary form.</p> <h3 id="tochar" class="hash-header">.toChar() <a href="#tochar" class="hash-link">#</a></h3> <p>Converts this value to character</p> <pre><code class="language-dart">final character = 97.toChar(); // a </code></pre> <h2 id="range" class="hash-header">range <a href="#range" class="hash-link">#</a></h2> <h3 id="rangeto" class="hash-header">rangeTo <a href="#rangeto" class="hash-link">#</a></h3> <p>Creates a range between two ints (upwards, downwards and with custom steps)</p> <pre><code class="language-dart">// upwards with default step size 1 for (final i in 1.rangeTo(5)) { print(i); // 1, 2, 3, 4, 5 } // downwards with custom step for (final i in 10.rangeTo(2).step(2)) { print(i); // 10, 8, 6, 4, 2 } </code></pre> <h2 id="function" class="hash-header">Function <a href="#function" class="hash-link">#</a></h2> <h3 id="partial-partial2-" class="hash-header">.partial(), .partial2() ... <a href="#partial-partial2-" class="hash-link">#</a></h3> <p>Applies some of the required arguments to a function and returns a function which takes the remaining arguments.</p> <pre><code class="language-dart">void greet(String firstName, String lastName) { print('Hi $firstName $lastName!'); } final greetStark = greet.partial('Stark'); greetStark('Sansa'); // Hi Sansa Stark! greetStark('Tony'); // Hi Tony Stark! </code></pre> <h2 id="file" class="hash-header">File <a href="#file" class="hash-link">#</a></h2> <h3 id="name" class="hash-header">.name <a href="#name" class="hash-link">#</a></h3> <p>Get the name and extension of a file.</p> <pre><code class="language-dart">final file = File('some/path/testFile.dart'); print(file.name); // testFile.dart print(file.nameWithoutExtension); // testFile </code></pre> <h3 id="appendtext" class="hash-header">.appendText() <a href="#appendtext" class="hash-link">#</a></h3> <p>Append text to a file.</p> <pre><code class="language-dart">await File('someFile.json').appendText('{test: true}'); </code></pre> <h3 id="iswithin" class="hash-header">.isWithin() <a href="#iswithin" class="hash-link">#</a></h3> <p>Checks if a file is inside a directory.</p> <pre><code class="language-dart">final dir = Directory('some/path'); File('some/path/file.dart').isWithin(dir); // true </code></pre> <h2 id="directory" class="hash-header">Directory <a href="#directory" class="hash-link">#</a></h2> <h3 id="filestring" class="hash-header">.file(String) <a href="#filestring" class="hash-link">#</a></h3> <p>References a file within a <code>Directory</code></p> <pre><code class="language-dart">Directory androidDir = Directory('flutter-app/android'); File manifestFile = androidDir.file("app/src/main/AndroidManifest.xml"); </code></pre> <p>References a directory within a <code>Directory</code></p> <h3 id="directorystring" class="hash-header">.directory(String) <a href="#directorystring" class="hash-link">#</a></h3> <pre><code class="language-dart">Directory androidDir = Directory('flutter-app/android'); Directory mainSrc = androidDir.directory("app/src/main"); </code></pre> <h3 id="containsfilesystementity-entity-bool-recursive--false" class="hash-header">.contains(FileSystemEntity entity, {bool recursive = false}) <a href="#containsfilesystementity-entity-bool-recursive--false" class="hash-link">#</a></h3> <p>Checks if a <code>Directory</code> contains a <code>FileSystemEntity</code>. This can be a <code>File</code> or a <code>Directory</code>.</p> <p>Use the <code>recursive</code> argument to include the subdirectories.</p> <pre><code class="language-dart">final File someFile = File('someFile.txt'); final Directory someDir = Directory('some/dir'); final Directory parentDir = Directory('parent/dir'); parentDir.contains(someFile); parentDir.contains(someDir); parentDir.contains(someFile, recursive: true); parentDir.contains(someDir, recursive: true); </code></pre> <p>This is the <code>async</code> method, which returns a <code>Future&lt;bool&gt;</code>.</p> <h3 id="containssyncfilesystementity-entity-bool-recursive--false" class="hash-header">.containsSync(FileSystemEntity entity, {bool recursive = false}) <a href="#containssyncfilesystementity-entity-bool-recursive--false" class="hash-link">#</a></h3> <p>Same as <code>.contains(FileSystemEntity entity, {bool recursive = false})</code> but synchronous. Returns a <code>bool</code>.</p> <h2 id="license" class="hash-header">License <a href="#license" class="hash-link">#</a></h2> <pre><code class="language-plain">Copyright 2019 Simon Leier Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. </code></pre> </section></div></div></div><aside class="detail-info-box"><a class="packages-scores" href="/packages/dartx/score"><div class="packages-score packages-score-like"><div class="packages-score-value -has-value"><span class="packages-score-value-number">948</span><span class="packages-score-value-sign"></span></div><div class="packages-score-label">likes</div></div><div class="packages-score packages-score-health"><div class="packages-score-value -has-value"><span class="packages-score-value-number">160</span><span class="packages-score-value-sign"></span></div><div class="packages-score-label">points</div></div><div class="packages-score packages-score-downloads" title="Number of downloads of this package during the past 30 days"><div class="packages-score-value -has-value"><span class="packages-score-value-number">488k</span><span class="packages-score-value-sign"></span></div><div class="packages-score-label">downloads</div></div></a><h3 class="title">Publisher</h3><p><a href="/publishers/simc.dev"><img class="-pub-publisher-shield filter-invert-on-dark" src="/static/hash-pb35dgsm/img/material-icon-verified.svg" alt="verified publisher" width="14" height="14" title="Published by a pub.dev verified publisher"/>simc.dev</a></p><h3 class="title">Weekly Downloads</h3><div id="-weekly-downloads-sparkline" class="weekly-downloads-sparkline" data-widget="weekly-sparkline" data-weekly-sparkline-points="gM7QZ3K/AQAcrAEA66gBABK7AQCSpAEAe4oBAAyZAQAhmQEAtIEBAIgwAQC3pgAAOx4BAAGjAQCMjwEAGYsBADmbAQBLjgEAgokBAOFpAQBojQEAfKMBAKhvAQCsdwEAZXwBAHuEAQCOZAEACXIBAMRZAQBpZgEANUsBAGZQAQDUUAEAn0MBABY3AQBySQEAWjQBAAsxAQASQgEAAj0BAE9KAQBQQgEA4UoBAME+AQDuJwEA5xYBAIgOAQAxRgEABzIBAOErAQD5HgEAd0sBABNDAQA="></div><h3 class="title pkg-infobox-metadata">Metadata</h3><p>Superpowers for Dart. Collection of useful static extension methods.</p><p><a class="link" href="https://github.com/leisim/dartx" rel="ugc">Repository (GitHub)</a><br/></p><h3 class="title">Documentation</h3><p><a class="link" href="/documentation/dartx/latest/">API reference</a><br/></p><h3 class="title">License</h3><p><img class="inline-icon-img filter-invert-on-dark" src="/static/hash-pb35dgsm/img/material-icon-balance.svg" alt="" width="14" height="14" role="presentation"/>Apache-2.0 (<a href="/packages/dartx/license">license</a>)</p><h3 class="title">Dependencies</h3><p><a href="/packages/characters" title="^1.1.0">characters</a>, <a href="/packages/collection" title="^1.15.0">collection</a>, <a href="/packages/crypto" title="^3.0.0">crypto</a>, <a href="/packages/meta" title="^1.3.0">meta</a>, <a href="/packages/path" title="^1.8.0">path</a>, <a href="/packages/time" title="^2.0.0">time</a></p><h3 class="title">More</h3><p><a href="/packages?q=dependency%3Adartx" rel="nofollow">Packages that depend on dartx</a></p></aside></div><script type="application/ld+json">{"@context":"http\u003a\u002f\u002fschema.org","@type":"SoftwareSourceCode","name":"dartx","version":"1.2.0","description":"dartx - Superpowers for Dart. Collection of useful static extension methods.","url":"https\u003a\u002f\u002fpub.dev\u002fpackages\u002fdartx","dateCreated":"2014-07-03T14\u003a37\u003a03.532Z","dateModified":"2023-07-18T12\u003a22\u003a08.941213Z","programmingLanguage":"Dart","image":"https\u003a\u002f\u002fpub.dev\u002fstatic\u002fimg\u002fpub-dev-icon-cover-image.png","license":"https\u003a\u002f\u002fpub.dev\u002fpackages\u002fdartx\u002flicense"}</script></div><div class="detail-metadata"><h3 class="detail-metadata-title"><span class="detail-metadata-toggle">←</span> Metadata</h3><div class="detail-info-box"><a class="packages-scores" href="/packages/dartx/score"><div class="packages-score packages-score-like"><div class="packages-score-value -has-value"><span class="packages-score-value-number">948</span><span class="packages-score-value-sign"></span></div><div class="packages-score-label">likes</div></div><div class="packages-score packages-score-health"><div class="packages-score-value -has-value"><span class="packages-score-value-number">160</span><span class="packages-score-value-sign"></span></div><div class="packages-score-label">points</div></div><div class="packages-score packages-score-downloads" title="Number of downloads of this package during the past 30 days"><div class="packages-score-value -has-value"><span class="packages-score-value-number">488k</span><span class="packages-score-value-sign"></span></div><div class="packages-score-label">downloads</div></div></a><h3 class="title">Publisher</h3><p><a href="/publishers/simc.dev"><img class="-pub-publisher-shield filter-invert-on-dark" src="/static/hash-pb35dgsm/img/material-icon-verified.svg" alt="verified publisher" width="14" height="14" title="Published by a pub.dev verified publisher"/>simc.dev</a></p><h3 class="title">Weekly Downloads</h3><div id="-weekly-downloads-sparkline" class="weekly-downloads-sparkline" data-widget="weekly-sparkline" data-weekly-sparkline-points="gM7QZ3K/AQAcrAEA66gBABK7AQCSpAEAe4oBAAyZAQAhmQEAtIEBAIgwAQC3pgAAOx4BAAGjAQCMjwEAGYsBADmbAQBLjgEAgokBAOFpAQBojQEAfKMBAKhvAQCsdwEAZXwBAHuEAQCOZAEACXIBAMRZAQBpZgEANUsBAGZQAQDUUAEAn0MBABY3AQBySQEAWjQBAAsxAQASQgEAAj0BAE9KAQBQQgEA4UoBAME+AQDuJwEA5xYBAIgOAQAxRgEABzIBAOErAQD5HgEAd0sBABNDAQA="></div><h3 class="title pkg-infobox-metadata">Metadata</h3><p>Superpowers for Dart. Collection of useful static extension methods.</p><p><a class="link" href="https://github.com/leisim/dartx" rel="ugc">Repository (GitHub)</a><br/></p><h3 class="title">Documentation</h3><p><a class="link" href="/documentation/dartx/latest/">API reference</a><br/></p><h3 class="title">License</h3><p><img class="inline-icon-img filter-invert-on-dark" src="/static/hash-pb35dgsm/img/material-icon-balance.svg" alt="" width="14" height="14" role="presentation"/>Apache-2.0 (<a href="/packages/dartx/license">license</a>)</p><h3 class="title">Dependencies</h3><p><a href="/packages/characters" title="^1.1.0">characters</a>, <a href="/packages/collection" title="^1.15.0">collection</a>, <a href="/packages/crypto" title="^3.0.0">crypto</a>, <a href="/packages/meta" title="^1.3.0">meta</a>, <a href="/packages/path" title="^1.8.0">path</a>, <a href="/packages/time" title="^2.0.0">time</a></p><h3 class="title">More</h3><p><a href="/packages?q=dependency%3Adartx" rel="nofollow">Packages that depend on dartx</a></p></div><p class="detail-lead-back"><a class="detail-metadata-toggle">Back</a></p></div><div id="-screenshot-carousel" class="carousel"><fab id="-carousel-prev" class="mdc-fab carousel-prev carousel-nav" data-mdc-auto-init="MDCRipple" title="Previous" data-ga-click-event="screenshot-carousel-prev-click" tabindex="0"><div class="mdc-fab__ripple"></div><img class="mdc-fab__icon" src="/static/hash-pb35dgsm/img/keyboard_arrow_left.svg" alt="previous" width="24" height="24" aria-hidden="true"/></fab><div id="-image-container" class="image-container"></div><fab id="-carousel-next" class="mdc-fab carousel-next carousel-nav" data-mdc-auto-init="MDCRipple" title="Next" data-ga-click-event="screenshot-carousel-next-click" tabindex="0"><div class="mdc-fab__ripple"></div><img class="mdc-fab__icon" src="/static/hash-pb35dgsm/img/keyboard_arrow_right.svg" alt="next" width="24" height="24" aria-hidden="true"/></fab><p id="-screenshot-description" class="screenshot-description"></p></div></main><footer class="site-footer"><a class="link" href="https://dart.dev/">Dart language</a><a class="link sep" href="/report?subject=package%3Adartx&amp;url=https%3A%2F%2Fpub.dev%2Fpackages%2Fdartx">Report package</a><a class="link sep" href="/policy">Policy</a><a class="link sep" href="https://www.google.com/intl/en/policies/terms/">Terms</a><a class="link sep" href="https://developers.google.com/terms/">API Terms</a><a class="link sep" href="/security">Security</a><a class="link sep" href="https://www.google.com/intl/en/policies/privacy/">Privacy</a><a class="link sep" href="/help">Help</a><a class="link icon sep" href="/feed.atom"><img class="inline-icon" src="/static/hash-pb35dgsm/img/rss-feed-icon.svg" alt="RSS" width="20" height="20" title="RSS/atom feed"/></a><a class="link icon github_issue" href="https://github.com/dart-lang/pub-dev/issues/new"><img class="inline-icon" src="/static/hash-pb35dgsm/img/bug-report-white-96px.png" alt="bug report" width="20" height="20" title="Report an issue with this site"/></a></footer><script src="/static/hash-pb35dgsm/highlight/highlight-with-init.js" defer="defer"></script></body></html>

Pages: 1 2 3 4 5 6 7 8 9 10