如果你发现缺少某个扩展,请提出问题或拉取请求
在此页面上你可以找到一些扩展。查看文档以了解所有扩展。
在你的 pubspec.yaml
中添加以下内容:
dependencies: dartx: any
导入库后,你就可以使用这些扩展了。
import 'package:dartx/dartx.dart'; final slice = [1, 2, 3, 4, 5].slice(1, -2); // [2, 3, 4]
返回 start
(包含)和 end
(包含)之间索引的元素。
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]
按多个属性对列表进行排序。
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) => dog.name) .thenByDescending((dog) => dog.age); // Bark, Charlie (7), Charlie (2), Cookie, Tom
从列表中获取不同的元素。
final list = ['this', 'is', 'a', 'test']; final distinctByLength = list.distinctBy((it) => it.length); // ['this', 'is', 'a']
从集合中的所有集合获取所有元素的新的惰性 Iterable
。
final nestedList = [[1, 2, 3], [4, 5, 6]]; final flattened = nestedList.flatten(); // [1, 2, 3, 4, 5, 6]
当两个元素匹配谓词时,对条目进行分块:
final list = [1, 2, 4, 9, 10, 11, 12, 15, 16, 19, 20, 21]; final increasingSubSequences = list.chunkWhile((a, b) => a + 1 == b); // increasingSubSequences = [[1, 2], [4], [9, 10, 11, 12], [15, 16], [19, 20, 21]]
splitWhen
是 chunkWhile
的反面,每当谓词不匹配时就开始一个新的块。
通过使用提供的 builderAction
填充新创建的 StringBuffer
来构建新字符串,然后将其转换为 String
。
final word = buildString((sb) { for (var i = 0; i < 10; i++) { sb.write(i); } }); // 0123456789
为任何整数返回 String
类型的序数
final a = 1.ordinal(); // 1st final b = 108.ordinal(); // 108th
返回字符串的副本,将其第一个字母大写,如果为空或已经以大写字母开头,则返回原始字符串。
final word = 'abcd'.capitalize(); // Abcd final anotherWord = 'Abcd'.capitalize(); // Abcd
返回字符串的副本,将其第一个字母小写,如果为空或已经以小写字母开头,则返回原 始字符串。
final word = 'abcd'.decapitalize(); // abcd final anotherWord = 'Abcd'.decapitalize(); // abcd
如果字符串是 ASCII 编码的,则返回 true
。
final isAscii = 'abc123 !,.~'.isAscii; // true final isNotAscii = '§3'.isAscii; // false
如果此字符串为空或仅由空白字符组成,则返回 true
。
final notBlank = ' .'.isBlank; // false final blank = ' '.isBlank; // true
如果字符串可以解析为双精度浮点数,则返回 true
。
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
如果字符串可以解析为整数,则返回 true
。
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
如果字符串是 Latin 1 编码的,则返回 true
。
final isLatin1 = '§Êü'.isLatin1; // true final isNotLatin1 = 'ő'.isLatin1; // false
如果整 个字符串都是小写,则返回 true
。
final a = 'abc'.isLowerCase; // true final b = 'abC'.isLowerCase; // false final c = ' '.isLowerCase; // true final d = ''.isLowerCase; // false
如果此字符串不为空且包含除空白字符以外的字符,则返回 true
。
final blank = ' '.isNotBlank; // false final notBlank = ' .'.isNotBlank; // true
如果字符串为 null
或为空,则返回 true
。
final isNull = null.isNullOrEmpty; // true final isEmpty = ''.isNullOrEmpty; // true final isBlank = ' '.isNullOrEmpty; // false final isLineBreak = '\n'.isNullOrEmpty; // false
如果字符串既不是 null
也不为空,则返回 true
。
final isNull = null.isNullOrEmpty; // true final isEmpty = ''.isNullOrEmpty; // true final isBlank = ' '.isNullOrEmpty; // false final isLineBreak = '\n'.isNullOrEmpty; // false
如果字符串为 null
或为空白,则返回 true
。
final isNull = null.isNullOrBlank; // true final isEmpty = ''.isNullOrBlank; // true final isBlank = ' '.isNullOrBlank; // true final isLineBreak = '\n'.isNullOrBlank; // true final isFoo = ' foo '.isNullOrBlank; // false
如果字符串既不是 null
也不为空白,则返回 true
。
final isNull = null.isNullOrBlank; // true final isEmpty = ''.isNullOrBlank; // true final isBlank = ' '.isNullOrBlank; // true final isLineBreak = '\n'.isNullOrBlank; // true final isFoo = ' foo '.isNullOrBlank; // true
如果整个字符串都是大写,则返回 true
。
final a = 'ABC'.isUpperCase; // true final b = 'ABc'.isUpperCase; // false final c = ' '.isUpperCase; // true final d = ''.isUpperCase; // false
计算 MD5 摘要并将结果作为十六进制数字字符串返回。
final a = 'abc'.md5; // 900150983cd24fb0d6963f7d28e17f72 final b = 'ഐ⌛酪Б👨👨👧👦'.md5; // c7834eff7c967101cfb65b8f6d15ad46
使用特定的编码方案将字符串转换为 application/x-www-form-urlencoded 格式。
const originalUrl = 'Hello Ladies + Gentlemen, a signed OAuth request!'; final encodedUrl = originalUrl.urlEncode; // 'Hello%20Ladies%20+%20Gentlemen,%20a%20signed%20OAuth%20request!'
使用特定的编码方案解码 application/x-www-form-urlencoded 字符串。
const encodedUrl = 'Hello%20Ladies%20+%20Gentlemen,%20a%20signed%20OAuth%20request!'; final decodedUrl = encodingUrl.urlDecode; // 'Hello Ladies + Gentlemen, a signed OAuth request!'
从给定字符串中删除前缀、后缀或两者:
final name = 'James Bond'.removePrefix('James '); // Bond final milliseconds = '100ms'.removeSuffix('ms'); // 100 final text = '<p>Some HTML</p>' .removeSurrounding(prefix: '<p>', suffix: '</p>'); // Some HTML
返回一个字符顺序相反的新字符串。
final emptyString = ''.reversed; // '' final reversed = 'abc🤔'.reversed; // '🤔cba'
返回一个新的子字符串,包含从 [start] 到 [end] 的所有字符(包括这两个索引)。
如果省略 [end],则将其设置为 lastIndex
。
final sliceOne = 'awesomeString'.slice(0,6)); // awesome final sliceTwo = 'awesomeString'.slice(7)); // String
将字符串解析为 double
并返回结果,如果字符串不是有效的数字表示,则返回 null
。
final numOne = '1'.toDoubleOrNull(); // 1.0 final numTwo = '1.2'.toDoubleOrNull(); // 1.2 final blank = ''.toDoubleOrNull(); // null
将字符串解析为整数并返回结果。基数(进制)默认为 10。如果解析失败,则抛出 FormatException
。
final a = '1'.toInt(); // 1 final b = '100'.toInt(radix: 2); // 4 final c = '100'.toInt(radix: 16); // 256 final d = '1.0'.toInt(); // 抛出 FormatException
将字符串解析为整数,如果不是数字则返回 null
。
final number = '12345'.toIntOrNull(); // 12345 final notANumber = '123-45'.toIntOrNull(); // null
将字符串转换为 UTF-8 编码。
final emptyString = ''.toUtf8(); // [] final hi = 'hi'.toUtf8(); // [104, 105] final emoji = '😄'.toUtf8(); // [240, 159, 152, 132]
将字符串转换为UTF-16编码。
final emptyString = ''.toUtf16(); // [] final hi = 'hi'.toUtf16(); // [104, 105] final emoji = '😄'.toUtf16(); // [55357, 56836]
如果字符串不为null
则返回该字符串,否则返回空字符串。
String? nullableStr; final str = nullableStr.orEmpty(); // ''
如果此字符序列匹配给定的正则表达式,则返回true
。
print('as'.matches(RegExp('^.s\$'))) // true print('mst'.matches(RegExp('^.s\$'))) // false
Dartx导出了@jogboms优秀的⏰ time.dart包,因此你可以执行以下操作:
int secondsInADay = 1.days.inSeconds; Duration totalTime = [12.5.seconds, 101.milliseconds, 2.5.minutes].sum(); DateTime oneWeekLater = DateTime.now() + 1.week;
查看⏰ time.dart获取更多信息和示例。
确保该值在指定范围内。
final numberInRange = 123.coerceIn(0, 1000); // 123 final numberOutOfRange = -123.coerceIn(0, 1000); // 0
将此值转换为二进制形式。
将此值转换为字符
final character = 97.toChar(); // a
在两个整数之间创建范围(向上、向下和自定义步长)
// 向上,默认步长为1 for (final i in 1.rangeTo(5)) { print(i); // 1, 2, 3, 4, 5 } // 向下,自定义步长 for (final i in 10.rangeTo(2).step(2)) { print(i); // 10, 8, 6, 4, 2 }
将一些必需的参数应用于函 数,并返回一个接受剩余参数的函数。
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!
获取文件的名称和扩展名。
final file = File('some/path/testFile.dart'); print(file.name); // testFile.dart print(file.nameWithoutExtension); // testFile
向文件追加文本。
await File('someFile.json').appendText('{test: true}');
检查文件是否在目录内。
final dir = Directory('some/path'); File('some/path/file.dart').isWithin(dir); // true
引用Directory
内的文件
Directory androidDir = Directory('flutter-app/android'); File manifestFile = androidDir.file("app/src/main/AndroidManifest.xml");
引用Directory
内的目录
Directory androidDir = Directory('flutter-app/android'); Directory mainSrc = androidDir.directory("app/src/main");
检查Directory
是否包含FileSystemEntity
。这可以是File
或Directory
。
使用recursive
参数包含子目录。
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);
这是异步方法,返回Future<bool>
。
与.contains(FileSystemEntity entity, {bool recursive = false})
相同,但是同步的。返回bool
。
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.
AI辅助编程,代码自动修复
Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。
最强AI数据分析助手
小浣熊家族Raccoon,您的AI智能助手,致力于通过先进的人工智能技术,为用户提供高效、便捷的智能服务。无论是日常咨询还是专业问题解答,小浣熊都能以快速、准确的响应满足您的需求,让您的生活更加智能便捷。
像人一样思考的AI智能体
imini 是一款超级AI智能体,能根据人类指令,自主思考、自主完成、并且交付结果的AI智能体。
AI数字人视频创作平台
Keevx 一款开箱即用的AI数字人视频创作平台,广泛适用于电商广告、企业培训与社媒宣传,让全球企业与个人创作者无需拍摄剪辑,就能快速生成多语言、高质量的专业视频。
一站式AI创作平台
提供 AI 驱动的图片、视频生成及数字人等功能,助力创意创作
AI办公助手,复杂任务高效处理
AI办公助手,复杂任务高效处理。办公效率低?扣子空间AI助手支持播客生成、PPT制作、网页开发及报告写作,覆盖科研、商业、舆情等领域的专家Agent 7x24小时响应,生活工作无缝切换,提升50%效率!
AI小说写作助手,一站式润色、改写 、扩写
蛙蛙写作—国内先进的AI写作平台,涵盖小说、学术、社交媒体等多场景。提供续写、改写、润色等功能,助力创作者高效优化写作流程。界面简洁,功能全面,适合各类写作者提升内容品质和工作效率。
全能AI智能助手,随时解答生活与工作的多样问题
问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。
实时语音翻译/同声传译工 具
Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。
一键生成PPT和Word,让学习生活更轻松
讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内 容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。
最新AI工具、AI资讯
独家AI资源、AI项目落地
微信扫一扫关注公众号