【问题标题】:Flutter app: Can't return value of type "Either<LeftType, RightType>" from method because it has a return type of "Either<LeftType, RightType>"Flutter 应用程序:无法从方法返回类型为“Either<LeftType, RightType>”的值,因为它的返回类型为“Either<LeftType, RightType>”
【发布时间】:2020-11-17 22:29:44
【问题描述】:

无法弄清楚这一点 - 尤其是 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

在“execute”方法的返回语句中,此类中发生 Dart 分析器错误:

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

【问题讨论】:

    标签: flutter equatable dartz


    【解决方案1】:

    好的,找到根本原因 - 发布以防万一发生在其他人身上。

    这个问题的原因是因为不知何故,repo 文件上的 import 语句以不同的方式导入“failures.dart”,而不是它在“usecase”和违规文件中的导入方式。

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

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

    将这两个“包”选项对齐,分析器很高兴

    【讨论】:

      猜你喜欢
      • 2021-09-27
      • 2022-01-15
      • 2021-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-26
      • 2022-01-26
      • 2021-01-25
      相关资源
      最近更新 更多