gpt4 book ai didi

flutter - 编译时间错误 : Type arguments in partial instantiations must be instantiated and are therefore not allowed to depend on type parameters

转载 作者:IT王子 更新时间:2023-10-29 06:52:36 26 4
gpt4 key购买 nike

当我尝试调用 listenEventReducer 时,出现了这个奇怪的错误。该方法知道类型,但仍然不确定我哪里出错了。

import 'package:test/test.dart';

enum ObjectChangeType { added, modified, removed }

typedef UpdateItem<T extends ListenEventItem> = T Function(T oldItem, T newItem);

T _defaultUpdate<T extends ListenEventItem>(T oldItem, T newItem) => newItem;

abstract class ListenEventItem {
String get id;

ObjectChangeType get changeType;
}

abstract class ListenEvent<T extends ListenEventItem> {
List<T> get data;

Map<ObjectChangeType, List<T>> get typedData {
return data.fold(
<ObjectChangeType, List<T>>{},
(Map<ObjectChangeType, List<T>> map, T element) {
map[element.changeType] = (map[element.changeType] ?? <T>[])..add(element);

return map;
},
);
}
}

Map<String, T> listenEventReducer<T extends ListenEventItem>(Map<String, T> state, ListenEvent<T> action,
[UpdateItem<T> update = _defaultUpdate]) {
update ??= _defaultUpdate;
final Map<String, T> b = Map<String, T>.from(state);

for (ObjectChangeType type in ObjectChangeType.values) {
final List<T> data = action.typedData[type];
if (data == null || data.isEmpty) {
continue;
}

if (type == ObjectChangeType.added) {
for (T item in data) {
b[item.id] == item;
}
} else if (type == ObjectChangeType.modified) {
for (T item in data) {
b[item.id] = update(b[item.id], item);
}
} else if (type == ObjectChangeType.removed) {
final List<String> ids = data.map((T it) => it.id).toList();
b.removeWhere((String id, _) => ids.contains(id));
}
}

return b;
}

class Data extends ListenEventItem {
Data(this.id, this.changeType, this.metadata);

final String metadata;

@override
final String id;

@override
final ObjectChangeType changeType;
}

class DataEvent extends ListenEvent<Data> {
DataEvent(this.data);

@override
final List<Data> data;
}

void main() {
final List<Data> items = List<Data>();
items.add(Data('0', ObjectChangeType.added, 'a'));
items.add(Data('1', ObjectChangeType.added, 'b'));
items.add(Data('0', ObjectChangeType.removed, 'a'));
items.add(Data('2', ObjectChangeType.added, 'c'));
items.add(Data('1', ObjectChangeType.modified, 'd'));
items.add(Data('3', ObjectChangeType.added, 'e'));
final DataEvent event = DataEvent(items);

final Map<String, Data> state = Map<String, Data>();
final Map<String, Data> newState = listenEventReducer<Data>(state, event);

expect(newState, isNotNull);
expect(newState.length, 3);
expect(newState['0'], isNull);
expect(newState['1'].metadata, 'd');
expect(newState['2'].metadata, 'c');
expect(newState['3'].metadata, 'e');
}

最佳答案

问题是 typedef和函数类型 _defaultUpdate不完全匹配并向编译器提供有关类型泛型的完整信息。

初始化时[UpdateItem<T> update = _defaultUpdate] ,编译器需要类型 T对于 UpdateItem 但它不知道是否 _defaultUpdate实际上正在返回 T其中 T extends ListenEventItem , _defaultUpdate可以返回任何类型 S其中 S extends ListenEventItem .

例如,假设您有两个扩展 ListenEventItem 的类

class A extends ListenEventItem {}

class B extends ListenEventItem {}

现在,_defaultUpdate函数可以返回 AB当没有传递通用参数时。当你尝试像这样初始化它时

UpdateItem<A> update = _defaultUpdate

编译器不知道是否 _defaultUpdate返回 AB使其在编译时失败。


您可以修复此问题,使 typedef 函数接受通用参数,这样编译器就知道期望任何扩展 ListenEventItem 的类

例子:

typedef UpdateItem = T Function<T extends ListenEventItem>(T oldItem, T newItem);

T _defaultUpdate<T extends ListenEventItem>(T oldItem, T newItem) => newItem;

完整代码:

import 'package:test/test.dart';

enum ObjectChangeType { added, modified, removed }

typedef UpdateItem = T Function<T extends ListenEventItem>(T oldItem, T newItem);

T _defaultUpdate<T extends ListenEventItem>(T oldItem, T newItem) => newItem;

abstract class ListenEventItem {
String get id;

ObjectChangeType get changeType;
}

abstract class ListenEvent<T extends ListenEventItem> {
List<T> get data;

Map<ObjectChangeType, List<T>> get typedData {
return data.fold(
<ObjectChangeType, List<T>>{},
(Map<ObjectChangeType, List<T>> map, T element) {
map[element.changeType] = (map[element.changeType] ?? <T>[])
..add(element);

return map;
},
);
}
}

Map<String, T> listenEventReducer<T extends ListenEventItem>(
Map<String, T> state, ListenEvent<T> action,
[UpdateItem update = _defaultUpdate]) {
update ??= _defaultUpdate;
final Map<String, T> b = Map<String, T>.from(state);

for (ObjectChangeType type in ObjectChangeType.values) {
final List<T> data = action.typedData[type];
if (data == null || data.isEmpty) {
continue;
}

if (type == ObjectChangeType.added) {
for (T item in data) {
b[item.id] = item;
}
} else if (type == ObjectChangeType.modified) {
for (T item in data) {
b[item.id] = update(b[item.id], item);
}
} else if (type == ObjectChangeType.removed) {
final List<String> ids = data.map((T it) => it.id).toList();
b.removeWhere((String id, _) => ids.contains(id));
}
}

return b;
}

class Data extends ListenEventItem {
Data(this.id, this.changeType, this.metadata);

final String metadata;

@override
final String id;

@override
final ObjectChangeType changeType;
}

class DataEvent extends ListenEvent<Data> {
DataEvent(this.data);

@override
final List<Data> data;
}

void main() {
final List<Data> items = List<Data>();
items.add(Data('0', ObjectChangeType.added, 'a'));
items.add(Data('1', ObjectChangeType.added, 'b'));
items.add(Data('0', ObjectChangeType.removed, 'a'));
items.add(Data('2', ObjectChangeType.added, 'c'));
items.add(Data('1', ObjectChangeType.modified, 'd'));
items.add(Data('3', ObjectChangeType.added, 'e'));
final DataEvent event = DataEvent(items);

final Map<String, Data> state = Map<String, Data>();
final Map<String, Data> newState = listenEventReducer<Data>(state, event);

test('Test', () {
expect(newState, isNotNull);
expect(newState.length, 3);
expect(newState['0'], isNull);
expect(newState['1'].metadata, 'd');
expect(newState['2'].metadata, 'c');
expect(newState['3'].metadata, 'e');
});
}

关于flutter - 编译时间错误 : Type arguments in partial instantiations must be instantiated and are therefore not allowed to depend on type parameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57051480/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com