0

私は現在、フラッターフレームワークとダーツを試していて、理解できない一見奇妙な動作に出くわしました。実際の問題が発生するコンテキストはもっと複雑ですが、非常に単純化されたショーケースでそれを再現することさえできました。

Stream<Either<String, int>> result1 = Stream.fromIterable([1, 2, 3, 4, 5])
    .map((number) => number < 4 ? Right(1) : Left('error'))
    .onErrorReturnWith((error) => Left('error'));

上記のサンプルは矛盾なくコンパイルされますが、以下のサンプルではコンパイル エラーが発生します。

エラー: タイプ 'Left<String, dynamic>' の値をタイプ 'Right<dynamic, int>' の変数に割り当てることはできません

Stream<Either<String, int>> result2 = Stream.fromIterable([1, 2, 3, 4, 5])
    .map((number) => Right(1))
    .onErrorReturnWith((error) => Left('error'));

この方法に光を当てることができる人はいますか?

#################################################### ######

もう一つの例:

  Future<Either<String, int>> someWebCall() {
Future<int> response = Future.delayed(Duration(milliseconds: 200), () {
  throw SocketException('Well... ...it happens you know...');
});

return response.then((number) {
  return number > 50.0 ? Right(number) : Left('number is too small...');
}).catchError((error) {
  return Left('are you already questioning the meaning of your life?');
});

}

これはコンパイルされますが、実行時エラーで終了します: タイプ 'Future' はタイプ 'Future<Either<String, int>>' のサブタイプではありません

次に、このアプローチを思い付くことができる限り多くのヒントをコンパイラーに提供しようとしました。

Future<Either<String, int>> someWebCall() {
Future<int> response = Future.delayed(Duration(milliseconds: 200), () {
  throw SocketException('Well... ...it happens you know...');
});

return response.then<Either<String, int>>((number) {
  return number > 50.0 ? Right(number) : Left('number is too small...') as Either<String, int>;
}).catchError((error) {
  return Left('are you already questioning the meaning of your life?') as Either<String, int>;
});

}

今、私は取得しています:型 'Left<String, dynamic>' は、型キャストの型 'Either<String, int>' のサブタイプではありません

私は本当にこれについて頭を包むことはできません

4

2 に答える 2