Flutter数据验证插件none_or的使用
Flutter数据验证插件none_or的使用
提供了新的类 (None<T>
),该类伪装为一个 Future<T>
以利用 Dart 中唯一可用的联合类型 FutureOr<T>
。你可以通过这种方式区分显式 null
值和默认的 None<T>
值。
不幸的是,Dart 语言缺乏真正的联合类型,并且不允许扩展 FutureOr<T>
的功能。因此,此包采用了一些变通方法,使用 NoneOr<T>?
类型别名作为可能为 T
、null
或 None
(这是一个新类,实现 Future
但完全不打算像 future
那样行为)的参数。
赞助
特别感谢我们的赞助商 Scrapp Inc 支持本包的开发!
功能
该包引入了一个新的类 None<T>
,它是 Future<T>
的有效实现,可以替代 FutureOr<T>
类型的参数或变量。None
类型包括一个静态方法 fallback
,可以从主要字段返回 T?
,当主要字段是 T?
或为 null
时。然而,如果主要字段是 None<T>
或 Future<T>
,它可以返回可选的 fallback
参数。
开始使用
在你的 pubspec.yaml
文件中添加该包:
dependencies:
none_or: latest
导入该包:
import 'package:none_or/none_or.dart';
以下是使用示例。
使用示例
该包预期的用途是在允许区分显式 null
值和省略的值方面。以下示例展示了如何使用 NoneOr<T>
来提升具有可空字段的类的 copyWith
函数:
import 'package:none_or/none_or.dart';
import 'package:flutter/foundation.dart';
/// 一个简单的不可变类,包含一个使用 NoneOr<T> 的 copyWith 函数,
/// 可以忽略传递给 copyWith 的隐式无变化值,同时尊重显式提供的值,包括 `null`
[@immutable](/user/immutable)
class _NoneOrExampleClass {
final bool nonNullableField;
final bool? nullableField;
const _NoneOrExampleClass({
required this.nonNullableField,
this.nullableField,
});
[@override](/user/override)
String toString() => 'Example($nonNullableField, $nullableField)';
/// 创建 [this] 的副本,用给定的字段更改
_NoneOrExampleClass copyWith({
bool? nonNullableField,
NoneOr<bool>? nullableField = const None(),
}) =>
_NoneOrExampleClass(
nonNullableField: nonNullableField ?? this.nonNullableField,
nullableField: None.fallback(nullableField, this.nullableField),
);
/// 创建 [this] 的副本,用给定的字段更改
_NoneOrExampleClass copyWithAlt({
NoneOr<bool> nonNullableField = const None(),
NoneOr<bool>? nullableField = const None(),
}) =>
_NoneOrExampleClass(
nonNullableField: None.fallback(nonNullableField, this.nonNullableField),
nullableField: None.fallback(nullableField, this.nullableField),
);
_NoneOrExampleClass copyWithAlt2({
NoneOr<bool> nonNullableField = const None(),
// 注意 NoneOr<bool?> 实际上与 NoneOr<bool>? 或 NoneOr<bool?>? 相同 - 使用你觉得最合适的语法。
// 在未来,我们可能会引入自定义的 linter 规则来决定在哪里放置 "?" 字符
NoneOr<bool?>? nullableField = const None(),
}) =>
_NoneOrExampleClass(
nonNullableField: None.fallback(nonNullableField, this.nonNullableField),
nullableField: None.fallback(nullableField, this.nullableField),
);
}
// 示例使用
void _exampleUsage() {
// 基线
_NoneOrExampleClass e0 = _NoneOrExampleClass(
nonNullableField: true,
nullableField: true,
); // Example(true, true)
// 使用 copyWith 进行复制:
// 对于非可空基础字段的简单示例:
// 1. 如果字段被省略,则假定为 null,并保持现有值(true)
_NoneOrExampleClass s1 = e0.copyWith(); // Example(true, true)
// 2. 如果字段标记为 null,则现有值保持不变(true)
_NoneOrExampleClass s2 = e0.copyWith(nonNullableField: null); // Example(true, true)
// 3. 如果字段提供了一个值,则覆盖现有值(false)
_NoneOrExampleClass s3 = e0.copyWith(nonNullableField: false); // Example(false, true)
// 对于可空基础字段的简单示例:
// 1. 如果字段被省略,则假定为 None(),并保持现有值(true)
_NoneOrExampleClass s1 = e0.copyWith(); // Example(true, true)
// 2. 如果字段标记为 null,则现有值被覆盖为 null(null)
_NoneOrExampleClass s2 = e0.copyWith(nullableField: null); // Example(true, null)
// 3. 如果字段提供了一个值,则覆盖现有值(false)
_NoneOrExampleClass s3 = e0.copyWith(nullableField: false); // Example(true, false)
}
更多关于Flutter数据验证插件none_or的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter数据验证插件none_or的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,none_or
并不是一个官方或广泛认知的 Flutter 数据验证插件。不过,在 Flutter 中进行表单验证时,我们通常会使用一些第三方库,比如 validator
或 flutter_form_builder
。这些库提供了丰富的验证功能,但没有一个特定的 none_or
函数。不过,我们可以自己实现类似的功能,即验证一个字段是否为空或者满足某些条件。
以下是一个使用 flutter_form_builder
库进行表单验证的示例,其中包含一个自定义验证器来模拟 none_or
的功能(即字段可以为空或者满足某个条件):
首先,确保在你的 pubspec.yaml
文件中添加 flutter_form_builder
依赖:
dependencies:
flutter:
sdk: flutter
flutter_form_builder: ^7.0.0 # 请检查最新版本
然后,在你的 Dart 文件中:
import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Form Builder Example'),
),
body: MyForm(),
),
);
}
}
class MyForm extends StatefulWidget {
@override
_MyFormState createState() => _MyFormState();
}
class _MyFormState extends State<MyForm> {
final _formKey = GlobalKey<FormBuilderState>();
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(16.0),
child: FormBuilder(
key: _formKey,
initialValue: {
'name': '',
},
autovalidateMode: AutovalidateMode.onUserInteraction,
child: Column(
children: <Widget>[
FormBuilderTextField(
name: 'name',
decoration: InputDecoration(labelText: 'Name'),
validators: [
FormBuilderValidators.compose([
// 自定义验证器:字段可以为空或者满足某个条件(例如长度大于3)
(value) {
if (value == null || value.isEmpty || value.length > 3) {
return null;
} else {
return 'Name must be empty or longer than 3 characters';
}
},
]),
],
),
FormBuilderSubmitButton(
decoration: InputDecoration(labelText: 'Submit'),
onPressed: () {
if (_formKey.currentState!.validate()) {
// 处理表单提交
_formKey.currentState!.save();
print('Form submitted!');
}
},
),
],
),
),
);
}
}
在这个示例中,我们使用了 FormBuilderTextField
来创建一个文本字段,并通过 validators
属性添加了一个自定义验证器。这个验证器检查字段是否为空或者长度是否大于3。如果字段为空或者满足长度条件,则验证通过;否则,返回错误信息。
这个示例展示了如何在 Flutter 中使用 flutter_form_builder
库进行表单验证,并自定义了一个验证器来模拟 none_or
的功能。希望这能帮助你理解如何在 Flutter 中进行复杂的数据验证。