0

我正在尝试使用本教程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 但仍然出现错误。

任何帮助将非常感激!

4

2 回答 2

0

这对我有用。此解决方案归功于 Peter Thompson(可在本教程的评论部分找到)

  1. 编辑 pub 规范中的 sdk 值以启用健全的 null 安全性:
    environment:
      sdk: ">=2.12.0 <3.0.0"
  1. 使用 0.14.2(或以上)版本的 freezes 和 freezed 注解:
    dependencies:
      flutter:
        sdk: flutter
      dartz: ^0.9.0-dev.6
      freezed_annotation: ^0.14.2
    
    dev_dependencies:
      build_runner:
      freezed: ^0.14.2
  1. 添加一个“?” 在 failedValue 的类型声明之后:
    @freezed
    abstract class ValueFailure<T> with _$ValueFailure<T> {
      const factory ValueFailure.invalidEmail({
        @required T? failedValue,
      }) = InvalidEmail<T>;
      const factory ValueFailure.shortPassword({
        @required T? failedValue,
      }) = ShortPassword<T>;
    }
于 2021-09-28T15:12:37.820 回答
0

freezed我已经尝试过您的代码,解决方案是设置in的特定版本pubspec.yaml

dependencies:
  freezed_annotation: ^0.12.0
dev_dependencies:
  freezed: ^0.12.2

无需覆盖analyzer包版本。

这适用于 Flutter 2.0.3 和 Dart 2.12.2。

于 2021-03-23T10:30:17.290 回答