
前言写 Dart / Flutter 时你一定遇到过这些场景字符串转 int 要写int.parse(s)还担心抛异常日期格式化到处写DateFormat列表想要个chunked还得自己写循环Map 想取个不存在的 key 要加一大堆containsKey判断……这些小操作本身不难但每天写十几次代码就变得啰嗦且容易出错。这就是我开源dart_common_extensions的原因。项目地址github.com/ceeyang/dart_common_extensionsPub 地址pub.dev/packages/dart_common_extensions⭐150 扩展方法覆盖 String / DateTime / List / Map / Num / Enum / Object 零运行时依赖仅 decimal intl 100 单元测试 完整 API 文档 示例代码安装dependencies: dart_common_extensions: ^0.0.9# Flutter 项目flutter pub get# 纯 Dart 项目dart pub get核心功能一览 Object Extensions — 函数式编程小工具Kotlin 开发者会非常熟悉let和alsoDart 里也能用了// let对非空对象执行操作并返回结果 final lengthuserInput?.let((it)it.length)??0;//also对对象执行副作用并返回对象本身 someObject.also((it)print(Processing:$it));// 空安全判断 print(someObject.isNull);//true/falseprint(someObject.isNotNull);//true/false String Extensions — 字符串处理的瑞士军刀这是这个包里最常用的部分。类型转换、校验、格式化、大小写变换一行搞定类型转换12.toInt;//1212.34.toDouble;//12.342023-01-01.toDate;// DateTime(2023,1,1){key:value}.toJson;//{key: value}1,2,3.toIntList;//[1,2,3]校验方法13912345678.isChineseMobile;//trueuserexample.com.isEmail;//true192.168.1.1.isIP;//true{a:1}.isJson;//trueSGVsbG8.isBase64;//truehttps://example.com.isValidUrl;// true支持 localhost、IP、自定义端口AFFE.isValidHex;//true大小写转换hello_world.toCamelCase;// helloWorldhelloWorld.toSnakeCase;// hello_worldhello-world.toKebabCase;// hello-worldhello world.toTitleCase;// Hello World字符串操作This is a long text.truncate(10);//This is a ...userexample.com.substringBefore();//userhello.capitalize;//HelloHello World.removeAllSpaces;//HelloWorld DateTime Extensions — 日期时间不头疼格式化final nowDateTime.now();now.ymd;//2026-07-15now.dmy;//15-07-2026now.iso8601;//2026-07-15T12:00:00now.fullDateTime;//2026-07-15 12:00:00now.monthYear;//July 2026now.shortDate;//7/15/2026locale dependent now.longDate;//July 15, 2026locale dependent now.time;//12:00:00字符串也可以直接格式化2023-01-01.ymd;//2023-01-012023-01-01.fullDateTime;//2023-01-01 00:00:001672531200000.ymd;//2023-01-01时间戳也支持日期计算now.startOfDay;// 当天 00:00:00.000 now.endOfDay;// 当天23:59:59.999 now.isToday;//true/falsenow.nextDay;// 明天 now.isLeapYear;// 是否闰年 now.daysInMonth;// 当月天数 // 工作日计算 DateTime fridayDateTime(2023,1,6);friday.addBusinessDays(1);// 下周一跳过周末 friday.subtractBusinessDays(1);// 上周四 DateTime(2023,1,7).isWeekend;// true周六 List Iterable Extensions — 集合操作大补全安全访问[a,b,c].safeElementAt(5);// null不抛异常[1,2,3,4,5].firstWhereOrNull((e)e10);// null分组与变换[1,2,3,4,5].chunked(2);//[[1,2],[3,4],[5]][1,2,3,4].windowed(2);//[[1,2],[2,3],[3,4]][1,2].zip([a,b]);//[[1,a],[2,b]][1,2,3].mapIndexed((i, e)$i: $e);//[0: 1,1: 2,2: 3]统计聚合[1,2,3,4].sum;//10[1,2,3,4].average;//2.5[1,2,3,4].max;//4[1,2,3,4].min;//1[1,2,3,4].count((i)i2);//2实用方法[1,2,3].random;// 随机元素[1,2,3].shuffled;// 打乱后的新列表[1,2,3].groupBy((e)e.isEven ?even:odd);//{odd:[1,3],even:[2]}[null, null].isAllNull;//true️ Map Extensions — 安全又灵活var map{first:1,second:2};map.getOrDefault(third,0);//0不抛异常 map.getOrNull(third);// null map.toJsonString();//{first: 1, second: 2}// 过滤 map.pick([first]);//{first:1}map.omit([first]);//{second:2}map.filterKeys((k)k.startsWith(f));//{first:1}map.filterValues((v)v1);//{second:2}//合并 {a:1}.merge({a:2,b:3});//{a:2,b:3} {a:1}.merge({a:2},(v1,v2)v1v2);//{a:3}//变换 {a:1}.mapKeys((k,v)k.toUpperCase());//{A:1}{a:1}.mapValues((k,v)v1);//{a:2} Num Extensions — 数字也可以有语法糖时间 Duration5.seconds;// Duration(seconds:5)1.5.days;// Duration(hours:36)60.secondsDuration;//0:01:00.000000 await5.secondsDelay();// 延迟5秒格式化1024.toFileSize();//1.00 KB123456.78.toCurrency(symbol:€);//€123,456.781234.567.toMoney(decimalDigits:1);//1,234.6255.toHexString;//ff10.toBinaryString;//10100.1234.toPercentage();//12.34%数学1.rangeTo(5);//[1,2,3,4,5]5.rangeTo(1);//[5,4,3,2,1]1.sumTo(5);//152.power(3);//85.isPrime;//true2.isEven;//true Enum Extensions — 循环导航enum Status{active, inactive}Status.active.next(Status.values);// Status.inactive Status.inactive.previous(Status.values);// Status.active特别适合分页状态机、步骤导航等场景。为什么自己造这个轮子Dart 生态中其实不缺扩展库比如dartx、basics、supercharged等。选择自建一个有几个原因按需定制只收录日常真的会用到的扩展不做大而全的百宝箱中文友好内置了isChinese、isChineseMobile等中文场景校验简单透明代码量不大每个扩展方法都清晰可查遇到问题直接看源码比翻文档快零学习成本所有方法命名都是自解释的看一眼就知道干嘛用的项目状态与路线图当前版本0.0.92026年5月底初次发布已有 150 扩展方法和 100 单元测试。后续计划Num扩展货币格式化、范围生成、延迟List扩展windowed、zip、groupByString扩展百分比、Base64、URL 编码Iterable扩展补充更多懒加载操作Duration扩展人类可读格式Null扩展isNull/isNotNull顶层函数更多本地化日期格式欢迎提 Issue 或 PR 贡献你常用的扩展方法。结语dart_common_extensions不是什么惊天动地的项目它是一个工具箱——把每天写 Dart / Flutter 时那些又来一遍的操作封装起来让代码更短、更清晰、更安全。项目是完全开源的MIT 协议欢迎 Star、Issue 和 PR github.com/ceeyang/dart_common_extensions如果你也在写 Dart / Flutter不妨试一试加一行依赖就能省下不少样板代码。