I am trying to build email model with authentication using this tutorial https://www.youtube.com/watch?v=fdUwW0GgcS8&list=PLB6lc7nQ1n4iS5p-IezFFgqP6YvAJy84U&index=2
The code is:
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>;
}
However, I am having a number of issues with getting the freezed package to work properly.
First was receiving error about version conflicts between SDK and analyzer:
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
I added the following to pub spec.yaml which seemed to fix it:
dependency_overrides:
analyzer: ^0.41.1
However, now I receive the error below when I run 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.
I tried adding
analyzer:
enable-experiment:
- non-nullable
to the analysis_options.yaml based on some googling but still getting the error.
Any help would be much appreciated!