Flutter应用:无法从方法返回类型“ Either <LeftType,RightType>”的值,因为它的返回类型为“ Either <LeftType,RightType>”

时间:2020-07-28 13:06:05

标签: flutter equatable dartz

无法弄清楚这一点-特别是因为VSCode和Android Studio中的类型错误告诉我类型是相同的。

错误:

"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)'." 

版本:

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

在“执行”方法的返回语句上的此类中出现的飞镖分析器错误:

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
  }
}

故障类别:

import 'package:equatable/equatable.dart';

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

DailyThought类:

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];
}

回购类别:

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();
}

1 个答案:

答案 0 :(得分:1)

确定了根本原因-如果其他人碰到了,请发布。

此问题的原因是因为回购文件上的import语句以某种方式导入“ failures.dart”,而不是在“用例”和有问题的文件中导入。

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

vs

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

将“ package”选项和分析仪都对齐了