如何解决领域驱动设计:冻结的 dart 包给出“失败的断言布尔表达式不能为空”
我正在尝试使用本教程构建带有身份验证的电子邮件模型 https://www.youtube.com/watch?v=fdUwW0GgcS8&list=PLB6lc7nQ1n4iS5p-IezFFgqP6YvAJy84U&index=2
代码是:
import 'package:Flutter/cupertino.dart';
import 'package:dartz/dartz.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:Flutter/foundation.dart';
part 'email_address.freezed.dart';
//https://www.youtube.com/watch?v=fdUwW0GgcS8&list=PLB6lc7nQ1n4iS5p-IezFFgqP6YvAJy84U&index=2
// Make Illegal states unrepresentable
@immutable
class EmailAddress {
final Either<ValueFailure<String>,String> value;
factory EmailAddress(String input) {
//assert input is not null
assert(input != null);
//use private constructor
return EmailAddress._(
validateEmailAddress(input),);
}
// private constructor which will be used in factory constructor if email is valid.
const EmailAddress._(this.value);
@override
String toString() {
return 'EmailAddress($value)';
}
@override
bool operator ==(Object o) {
if (identical(this,o)) return true;
return o is EmailAddress && o.value == value;
}
@override
int get hashCode => value.hashCode;
}
// Use a REGEX expression to valid the email address.
Either<ValueFailure<String>,String> validateEmailAddress(String input) {
const emailRegex =
r"""^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+""";
if (RegExp(emailRegex).hasMatch(input)) {
// right side of Either gives String of valid email
return right(input);
} else {
// left side of either gives ValueFailure<String>
return left(ValueFailure.invalidEmail(FailedValue: input));
}
}
@freezed
abstract class ValueFailure<T> with _$ValueFailure<T> {
const factory ValueFailure.invalidEmail({
@required String FailedValue,}) = InvalidEmail<T>;
const factory ValueFailure.shortPassword({
@required String FailedValue,}) = ShortPassword<T>;
}
但是,我在使冻结的包正常工作时遇到了许多问题。
首先收到关于 SDK 和分析器之间版本冲突的错误:
Your current `analyzer` version may not fully support your current SDK version.
Please try upgrading to the latest `analyzer` by running `Flutter packages upgrade`.
Analyzer language version: 2.10.0
SDK language version: 2.12.0
我将以下内容添加到 pub spec.yaml 中,这似乎修复了它:
dependency_overrides:
analyzer: ^0.41.1
但是,现在我在运行 Flutter pub run build_runner watch
时收到以下错误,
[INFO] 7.8s elapsed,3/4 actions completed.
[SEVERE] freezed:freezed on lib/domain/auth/email_address.dart:
Failed assertion: boolean expression must not be null
[INFO] Running build completed,took 8.1s.
我尝试添加
analyzer:
enable-experiment:
- non-nullable
基于一些谷歌搜索到 analysis_options.yaml 但仍然得到错误。
任何帮助将不胜感激!
解决方法
我已经尝试了您的代码,解决方案是在 freezed
中设置特定版本的 pubspec.yaml
:
dependencies:
freezed_annotation: ^0.12.0
dev_dependencies:
freezed: ^0.12.2
无需覆盖 analyzer
软件包版本。
这适用于 Flutter 2.0.3 和 Dart 2.12.2。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。