簡體   English   中英

如何使用`combineReducers`在Dart中指定類型注釋?

[英]How can I specify type annotations in Dart with `combineReducers`?

我在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),
]);

我需要使用的正確類型是:

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),
]);

Dart 不支持聯合類型,因此無法正確鍵入操作.. 將需要“不太現實的類型”

您可以執行以下操作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM