0

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!

learyjk
  • 599
  • 1
  • 5
  • 14

2 Answers2

0

I've tried your code and the solution was to set a specific version of freezed in pubspec.yaml:

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

No need to override analyzer package version.

This works with Flutter 2.0.3 and Dart 2.12.2.

Mr. Goldberg
  • 101
  • 9
0

This worked for me. Credits to Peter Thompson for this solution (found in the comments section of the tutorial)

  1. edit to sdk value in pub spec to enable sound null safety:
    environment:
      sdk: ">=2.12.0 <3.0.0"
  1. Use the 0.14.2 (or above) version of freezes and freezed annotation:
    dependencies:
      flutter:
        sdk: flutter
      dartz: ^0.9.0-dev.6
      freezed_annotation: ^0.14.2
    
    dev_dependencies:
      build_runner:
      freezed: ^0.14.2
  1. Add a "?" after the type declaration for 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>;
    }
Mervin
  • 1
  • 1