【问题标题】:How can I specify type annotations in Dart with `combineReducers`?如何在 Dart 中使用 `combineReducers` 指定类型注释?
【发布时间】:2019-07-07 05:02:42
【问题描述】:

我在List 收到来自 linter 的消息:

指定类型注释

但是我不明白它想要什么类型。对于Map,我可以这样:

<String, String>{}

Map 位于末尾,我在开头指定类型,但找不到适合我的减速器和史诗的正确类型。

我当前的代码:

final profileReducer = combineReducers<Profile>([
  TypedReducer<Profile, UpdateProfileBalanceAction>(_updateProfileBalance),
  TypedReducer<Profile, FetchProfileResultAction>(_setProfile),
]);

和:

final epics = combineEpics<AppState>([
  TypedEpic<AppState, FetchProfileAction>(profileEpic),
]);

【问题讨论】:

  • Dart 不支持联合类型,所以这里不能有 real 类型。需要动态
  • :(这意味着我无法修复该警告?(我尝试使用dynamic,但没有成功)
  • 这意味着你不能有防错类型。但是您可以修复警告。我会发布它作为答案

标签: dart flutter


【解决方案1】:

我需要使用的正确类型是:

final Function profileReducer = combineReducers<Profile>(
<Profile Function(Profile, dynamic)>[
  TypedReducer<Profile, FetchProfileResultAction>(_setProfile),
]);

但为了使其更实用,我创建了一个类型:

import 'package:utgard/store/models/profile.dart';

typedef ProfileReducers = Profile Function(Profile, dynamic);

然后我可以在我的减速器中使用,如下所示:

final Function profileReducer = combineReducers<Profile>(<ProfileReducers>[
  TypedReducer<Profile, FetchProfileResultAction>(_setProfile),
]);

【讨论】:

    【解决方案2】:

    Dart 不支持联合类型,因此无法正确键入操作。需要“不太现实的类型”

    您可以执行以下操作:

    final profileReducer = combineReducers<Profile>(<TypedReducer<Profile, Action>>[
      TypedReducer<Profile, UpdateProfileBalanceAction>(_updateProfileBalance),
      TypedReducer<Profile, FetchProfileResultAction>(_setProfile),
    ]);
    

    【讨论】:

    • 我已经尝试过了,但是我收到以下错误:The argument type 'List&lt;TypedReducer&lt;Profile, dynamic&gt;&gt;' can't be assigned to the parameter type 'Iterable&lt;(Profile, dynamic) → Profile&gt;'.
    • 啊,combineReducers 直接取函数。 TypedReducer 不工作
    • 没关系!我找到了答案,并在这里发布,无论如何谢谢!
    猜你喜欢
    • 2019-10-15
    • 2021-07-14
    • 2017-05-03
    • 1970-01-01
    • 1970-01-01
    • 2020-01-13
    • 2018-08-01
    • 1970-01-01
    • 2016-09-10
    相关资源
    最近更新 更多