搜索插件、代码仓与 APP 分类

插件详情v10.0.1

dart_ping

适用于原生桌面和安卓应用的多平台网络 ping 工具。

AndroidiOSmacOSWindowsLinux
近 30 天下载
5.3万
喜欢数
134
Pub 分数
150

安装插件

flutter pub add dart_ping

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

dart_ping 是一个跨平台网络工具,为桌面和 Android 应用提供原生 ping 功能。它允许开发者通过向目标主机发送 ICMP 回显请求来检查网络连接状态,实时反馈可达性和延迟情况。专为跨平台使用设计,支持 Android、iOS、macOS、Windows 和 Linux,非常适合需要可靠网络诊断功能的应用程序。该包可通过简单的 add 命令无缝集成到 Flutter 项目中,并利用平台特定的实现以获得最佳性能。

适合场景

  • 检查设备到远程服务器的连接
  • 实时监控网络延迟
  • 在应用启动期间验证互联网访问
  • 将网络健康检查集成到监控工具中
  • 测试本地网络响应性

主要特点

  • 跨平台支持,涵盖主要操作系统
  • 原生 ICMP ping 实现
  • 低级别网络可达性测试
  • 与 Flutter 的简单 API 集成
  • 实时响应时间测量

适合对象

  • 网络诊断工具
  • 物联网设备连接应用
  • 需要网络健康检查的移动和桌面应用程序
  • 系统监控工具
  • 需要强大连接验证的应用程序

使用建议

  • 在某些平台上需要适当的系统权限
  • 可能被防火墙或网络策略阻止
  • 仅限基于ICMP的ping(无TCP/HTTP替代方案)
  • 平台特定的行为可能有所不同
  • 由于系统限制,不适用于高频轮询

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Dart Ping

Multi-platform network ping utility for Dart and Flutter applications, available via the pub.dev package repository.

One package, every platform

There is one package for every platform — add only dart_ping:

  • Windows, macOS, Linux, Android are served natively by the ping subprocess — no extra binaries.
  • iOS is served by a bundled native Swift ICMP engine driven over dart:ffi. The engine is compiled into the app by a Dart build hook only when the build target is iOS. It builds under Flutter's Swift Package Manager (SPM) mode with no CocoaPods Podfile, and requires the dart_ping 10.x SDK floor (Dart 3.10 / Flutter 3.38).

Pure-Dart consumers (CLI, server) are unaffected: with no iOS target, no Swift is compiled and no Flutter SDK is required.

Migrating from dart_ping_ios: remove the dart_ping_ios dependency, delete the DartPingIOS.register() call, and raise your SDK floor to the dart_ping 10.x baseline — no other source change, since the public Ping API is otherwise unchanged. Prior dart_ping_ios releases remain published on pub.dev for consumers who cannot adopt the raised floor; this repository no longer carries that package. See the CHANGELOG for the full 10.0.0 migration guide (including the sealed PingEvent stream and the ipv6 → IpVersion change).

Usage

A simple usage example:

import 'package:dart_ping/dart_ping.dart';

void main() async {
  // Create ping object with desired args
  final ping = Ping('google.com', count: 5);

  // Begin ping process and listen for output
  ping.stream.listen((event) {
    print(event);
  });
}

Instead of listening to a stream, you can perform a single ping and immediately return the first event like so:

final event = await Ping('google.com', count: 1).stream.first;

stream.first yields whatever [PingEvent] arrives first — a PingResponse on success, but a PingError if that first probe times out or the host is unreachable — so branch on the type (or be ready for first to surface an error) rather than assuming a successful reply.

To print the underlying ping command that will be used (useful for debugging):

print('Running command: ${ping.command}')

To prematurely halt the process:

await ping.stop()
The event stream

Ping.stream is a Stream<PingEvent> — a sealed union with three subtypes. Branch on the type with an exhaustive switch:

ping.stream.listen((event) {
  switch (event) {
    case PingResponse(): // a successful probe reply (seq, ttl, time, ip)
    case PingError():    // a probe/run error (may carry seq/ip)
    case PingSummary():  // the terminal run summary — the final event
  }
});

Every probe event also carries a nullable RoundTripStats? stats snapshot (min/avg/max/stddev/jitter so far), and PingSummary exposes both stats and a derived packetLoss getter, so you can drive a live latency/loss view without waiting for the summary.

Selecting a network interface

Ping(host, interface: ...) binds the ping to a specific interface, accepting either an interface name (e.g. eth0) or a local source IP address (e.g. 192.168.1.5). To discover the host's available interfaces, use listNetworkInterfaces() and feed one back into a Ping:

final interfaces = await listNetworkInterfaces();
// Pick one — by name or by a source address — then bind a ping to it.
final ping = Ping('dart.dev', interface: interfaces.first.name);
await for (final event in ping.stream) {
  print(event);
}

On Windows you must pass back a source address (e.g. interfaces.first.addresses.first.address), not the name, because Windows ping binds only by source address; a bare interface name is rejected there. A source address round-trips on every platform.

Address Family (IPv4 / IPv6)

The IP address family is chosen with the ipVersion parameter, an exclusive selection: IpVersion.ipv4 pings over IPv4 only and IpVersion.ipv6 over IPv6 only. There is no "prefer one family" or dual-stack mode — IpVersion.ipv4 excludes IPv6 rather than preferring it. The default is IpVersion.ipv4.

// IPv6 only (the system ping is invoked with the -6 family flag on
// Linux/Android and Windows; iOS IPv6 is served by dart_ping's own native
// engine. The macOS subprocess path is IPv4-only and raises an explicit
// error for an IPv6 selection.)
final ping = Ping('google.com', ipVersion: IpVersion.ipv6);

Migrating from the ipv6 boolean: the old ipv6: true / ipv6: false flag has been replaced by ipVersion. Map ipv6: true → ipVersion: IpVersion.ipv6, and ipv6: false (or omitting it) → ipVersion: IpVersion.ipv4.

IPv6-only networks (NAT64)

On an IPv6-only cellular network (NAT64/DNS64), an IPv4 literal such as 1.1.1.1 can be unreachable unless the platform synthesizes an IPv6 path to it. Ping exposes a nat64Synthesis option that is enabled by default, so an IPv4 literal keeps working on iOS without any code change. The active synthesis happens on iOS (via dart_ping's native engine); on the subprocess platforms (Windows, macOS, Linux, Android) the option is a no-op that leaves the ping command unchanged. Pass nat64Synthesis: false to opt out and restore raw pass-through.

Non-English Language Support

To support OS languages other than English, you can override the parser (Portuguese shown here):

final parser = PingParser(
    responseRgx: RegExp(r'de (?<ip>.*): bytes=(?:\d+) tempo=(?<time>\d+)ms TTL=(?<ttl>\d+)'),
    summaryRgx: RegExp(r'Enviados = (?<tx>\d+), Recebidos = (?<rx>\d+), Perdidos = (?:\d+)'),
    timeoutRgx: RegExp(r'host unreachable'),
    timeToLiveRgx: RegExp(r''),
    unknownHostStr: RegExp(r'A solicitação ping não pôde encontrar o host'),
  );

final ping = Ping('google.com', parser: parser);

On Windows installations, you can force the codepage (437) of the console instead of providing a custom parser:

final ping = Ping('google.com', forceCodepage: true);

To override the character encoding to ignore non-utf characters:

final ping = Ping('google.com', encoding: Utf8Codec(allowMalformed: true));
macOS Release Build with App Sandbox

When building in release mode with app sandbox enabled, you must ensure you add the following entitlements to the Release.entitlements file in your macos folder:

<key>com.apple.security.network.server</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>

Features and bugs

Please file feature requests and bugs at the issue tracker.

所属分类

依赖

依赖清单 5 项

源码仓库

使用此插件的项目