我是 Dart 的新手。目前我有一个重复项目列表,我想计算它们的出现次数并将其存储在地图中。
var elements = ["a", "b", "c", "d", "e", "a", "b", "c", "f", "g", "h", "h", "h", "e", "a"];
Run Code Online (Sandbox Code Playgroud)
我想要这样的结果:
{
"a": 3,
"b": 2,
"c": 2,
"d": 2,
"e": 2,
"f": 1,
"g": 1,
"h": 3
}
Run Code Online (Sandbox Code Playgroud)
我做了一些研究并找到了一个 JavaScript 解决方案,但我不知道如何将其转换为 Dart。
var counts = {};
your_array.forEach(function(x) { counts[x] = (counts[x] || 0)+1; });
Run Code Online (Sandbox Code Playgroud) 我正在 Flutter 中实现一个 Todo 应用程序。我需要在客户端中合并双重查询,以便在 Firestore 中执行 OR 请求。
一方面,我有以下执行双重查询的代码。
Future<Stream> combineStreams() async {
Stream stream1 = todoCollection
.where("owners", arrayContains: userId)
.snapshots()
.map((snapshot) {
return snapshot.documents
.map((doc) => Todo.fromEntity(TodoEntity.fromSnapshot(doc)))
.toList();
});
Stream stream2 = todoCollection
.where("contributors", arrayContains: userId)
.snapshots()
.map((snapshot) {
return snapshot.documents
.map((doc) => Todo.fromEntity(TodoEntity.fromSnapshot(doc)))
.toList();
});
return StreamZip(([stream1, stream2])).asBroadcastStream();
}
Run Code Online (Sandbox Code Playgroud)
另一方面,我有以下代码将使用 Bloc 模式执行视图更新。
Stream<TodosState> _mapLoadTodosToState(LoadTodos event) async* {
_todosSubscription?.cancel();
var res = await _todosRepository.todos(event.userId);
_todosSubscription = res.listen(
(todos) {
dispatch(
TodosUpdated(todos));
},
);
}
Run Code Online (Sandbox Code Playgroud)
我有以下错误。
flutter: Instance of …Run Code Online (Sandbox Code Playgroud)