代码是:
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 但仍然出现错误。
任何帮助将非常感激!