如何在Flutter中的BehaviorSubject中重置上一个值

时间:2019-01-25 17:54:29

标签: flutter rxdart

嗨,我在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); } } # Knapsack def k(items, w) if w == 0 || items.size < 1 return 0 end current_item = items.shift if current_item[1] > w return k(items, w) end right = current_item[0] + k(items, w - current_item[1]) left = k(items, w) return [right, left].max end 也总是返回最终值:

puts k([[5,10],[3,11],[7,25],[1,7]], 40)

1 个答案:

答案 0 :(得分:1)

您可以将其值显式设置为null。不知道那是否是个好习惯,但是效果很好。

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