gpt4 book ai didi

flutter - 如何在 Flutter 中重置 BehaviorSubject 中的最后一个值

转载 作者:IT王子 更新时间:2023-10-29 07:23:26 63 4
gpt4 key购买 nike

您好,我在 BLoC 类中使用 BehaviorSubject。我面临的主要问题是,即使在对其调用 drain()close() 之后,最终值仍然保留在 Subject 中。我该如何重置它?

完整项目链接:https://github.com/SAGARSURI/Goals

这是我的代码。这里 _title_goalMessage 总是返回最终值,即使在调用了 drain()close() 方法之后也是如此:

class GoalsBloc {
final _repository = Repository();
final _title = BehaviorSubject<String>();
final _goalMessage = BehaviorSubject<String>();
final _showProgress = BehaviorSubject<bool>();

Observable<String> get name => _title.stream.transform(_validateName);

Observable<String> get goalMessage =>
_goalMessage.stream.transform(_validateMessage);

Observable<bool> get showProgress => _showProgress.stream;

Function(String) get changeName => _title.sink.add;

Function(String) get changeGoalMessage => _goalMessage.sink.add;

final _validateMessage = StreamTransformer<String, String>.fromHandlers(
handleData: (goalMessage, sink) {
if (goalMessage.length > 10) {
sink.add(goalMessage);
} else {
sink.addError(StringConstant.goalValidateMessage);
}
});

final _validateName = StreamTransformer<String, String>.fromHandlers(
handleData: (String name, sink) {
if (RegExp(r'[!@#<>?":_`~;[\]\\|=+)(*&^%0-9-]').hasMatch(name)) {
sink.addError(StringConstant.nameValidateMessage);
} else {
sink.add(name);
}
});

void submit(String email) {
_showProgress.sink.add(true);
_repository
.uploadGoal(email, _title.value, _goalMessage.value)
.then((value) {
_showProgress.sink.add(false);
});
}

void extractText(var image) {
_repository.extractText(image).then((text) {
_goalMessage.sink.add(text);
});
}

Stream<DocumentSnapshot> myGoalsList(String email) {
return _repository.myGoalList(email);
}

Stream<QuerySnapshot> othersGoalList() {
return _repository.othersGoalList();
}

//dispose all open sink
void dispose() async {
await _goalMessage.drain();
_goalMessage.close();
await _title.drain();
_title.close();
await _showProgress.drain();
_showProgress.close();
}

//Convert map to goal list
List mapToList({DocumentSnapshot doc, List<DocumentSnapshot> docList}) {
if (docList != null) {
List<OtherGoal> goalList = [];
docList.forEach((document) {
String email = document.data[StringConstant.emailField];
Map<String, String> goals =
document.data[StringConstant.goalField] != null
? document.data[StringConstant.goalField].cast<String, String>()
: null;
if (goals != null) {
goals.forEach((title, message) {
OtherGoal otherGoal = OtherGoal(email, title, message);
goalList.add(otherGoal);
});
}
});
return goalList;
} else {
Map<String, String> goals = doc.data[StringConstant.goalField] != null
? doc.data[StringConstant.goalField].cast<String, String>()
: null;
List<Goal> goalList = [];
if (goals != null) {
goals.forEach((title, message) {
Goal goal = Goal(title, message);
goalList.add(goal);
});
}
return goalList;
}
}

//Remove item from the goal list
void removeGoal(String title, String email) {
return _repository.removeGoal(title, email);
}
}

最佳答案

您可以将其值显式设置为 null。不知道这是否是好的做法,但它工作得很好。

void drainStream(){ _title.value = null; }

关于flutter - 如何在 Flutter 中重置 BehaviorSubject 中的最后一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54370518/

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