flutterequatabledartz

Flutter app: Can't return value of type "Either<LeftType, RightType>" from method because it has a return type of "Either<LeftType, RightType>"


Can't figure this one out - especially as the type-error in VSCode and Android Studio is telling me that the types are identical.

Error:

"A value of type 'Either<Failure, DailyThought> (where Failure is defined in ...lib\core\error\failures.dart)' can't be returned from method 'execute' because it has a return type of 'Either<Failure, DailyThought> (where Failure is defined in ...lib\core\error\failures.dart)'." 

Versions:

Flutter 1.17.3 • channel stable • https://github.com/flutter/flutter.git
Framework • revision b041144f83 (8 weeks ago) • 2020-06-04 09:26:11 -0700
Engine • revision ee76268252
Tools • Dart 2.8.4
dartz: ^0.9.1
equatable: ^1.2.3

Dart analyzer error occurring in this class on the "execute" method's return statement:

import '.../features/daily_thought/domain/entities/daily_thought.dart';
import '.../features/daily_thought/domain/repositories/daily_thought_repository.dart';    
import '.../core/error/failures.dart';
import 'package:dartz/dartz.dart';

class GetLatestDailyThought {
  final DailyThoughtRepository repository;

  GetLatestDailyThought(this.repository);

  Future<Either<Failure, DailyThought>> execute() async {
    return await repository.getLatestDailyThought();  //ERROR
  }
}

Failure class:

import 'package:equatable/equatable.dart';

abstract class Failure extends Equatable {
  Failure([List properties = const <dynamic>[]]);
}

DailyThought class:

import 'package:equatable/equatable.dart';
import 'package:flutter/cupertino.dart';
import 'package:meta/meta.dart';

class DailyThought extends Equatable {
  final int id;
  final String dateOfThought;
  final String textOfThought;

  DailyThought({
    @required this.id,
    @required this.dateOfThought,
    @required this.textOfThought,
  });

  @override
  List<Object> get props => [id, dateOfThought, textOfThought];
}

Repo class:

import 'package:dartz/dartz.dart';
import '.../lib/core/error/failures.dart';
import 'package:pedros_daily_thought_app/features/daily_thought/domain/entities/daily_thought.dart';

abstract class DailyThoughtRepository {
  Future<Either<Failure, DailyThought>> getLatestDailyThought();
}

Solution

  • Ok found the root cause - posting in case it happens to someone else.

    The reason for this problem was because somehow the import statement on the repo file was importing "failures.dart" in a different manner to how it was imported on "usecase" and offending file.

    import 'file:///C:/code/.../core/error/failures.dart'
    

    vs

    import 'package:../core/error/failures.dart';
    

    Aligned these both the "package" option and the analyzer was happy