g_recaptcha_v3 包是适用于 Flutter Web 的插件,可将 Google reCAPTCHA v3(一种防垃圾信息和滥用保护服务)集成到 Web 应用中。它可在无需用户交互的情况下实现隐形验证挑战,帮助区分人类用户与机器人。该插件通过提供 Dart API,简化了在客户端获取评分和验证响应的实现过程。此插件专为 Web 平台设计,可与 Flutter 的 Web 部署无缝配合。
适合场景
保护网页表单免受垃圾信息侵害
保护登录和注册端点
防止自动化机器人攻击
增强API端点的安全性
监控可疑用户行为
主要特点
隐形 reCAPTCHA 集成
客户端分数验证
仅限网页平台
简单的 Dart API
支持服务器端验证
轻便且快速
适合对象
需要机器人保护的Flutter网页应用
集成 Google reCAPTCHA v3 的开发者
需要非侵入式验证码的项目
注重安全的Web应用
前端开发人员使用身份验证
使用建议
仅适用于网页平台
需要后端验证
需要 Google reCAPTCHA 站点密钥
依赖互联网连接
不适用于移动应用
原始 README
以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。
g_recaptcha_v3
Create Google reCAPTCHA v3 tokens for Flutter web. Google reCAPTCHA is a free service that protects your website from spam and abuse. Supports reCAPTCHA Enterprise, recaptcha.net, and WASM builds.
For development, add localhost as domain in reCaptcha console
Step 2
Add g_recaptcha_v3 to pubspec.yaml
flutter pub add g_recaptcha_v3
Optional: manual script tag
The plugin injects the reCAPTCHA script automatically — no index.html changes are needed. If you prefer to load it yourself (e.g. for an earlier load), add the script inside the web/index.html<head> tag, before the flutter.js / flutter_bootstrap.js script. The plugin detects the existing tag and will not add a second one.
<head><scriptsrc="https://www.google.com/recaptcha/api.js?render=<your Recaptcha v3 site key>"></script></head>
Development
1. GRecaptchaV3.ready()
The ready() method should be called before execute()
The ready() method should be called before execute()
import'package:g_recaptcha_v3/g_recaptcha_v3.dart';
voidgenerateToken() async {
String? token = awaitGRecaptchaV3.execute('<your_action>'); //--3print(token);
// send token to server and verify
}
String action - used to verify the string in backend. ( action docs )
token will be null if,
the web setup has any errors (see Error handling for diagnosable exceptions).
the method is called from a non-web platform.
execute() throws a GRecaptchaNotReadyException if ready() was never called.
3. Advanced options
bool ready = awaitGRecaptchaV3.ready(
"<your Recaptcha site key>",
showBadge: false, // hide the badge (see warning below)
useRecaptchaNet: true, // load script from recaptcha.net instead of google.com// (for regions where google.com is blocked, e.g. China)
useEnterprise: true, // use reCAPTCHA Enterprise (grecaptcha.enterprise)
badgeLanguage: 'fr', // badge language (hl param)
scriptNonce: '<nonce>', // CSP nonce for the injected <script> tag
);
// check readiness anywhereif (GRecaptchaV3.isReady) { ... }
4. Error handling
By default execute() returns null on failure. For diagnosable errors:
try {
String? token = awaitGRecaptchaV3.execute('login', throwOnError: true);
} onGRecaptchaNotReadyException {
// ready() was not called or did not succeed
} onGRecaptchaExecutionExceptioncatch (e) {
// the underlying grecaptcha.execute() call failedprint(e.message);
}