這是按下“保存”按鈕時在我的應用程式中呼叫的方法。該方法使用物件訪問ChangeNotifier實體Provider并更新底層模型。
/// Handle the user pressing the Submit button within the dialog.
void _saveChanges() {
// HANDLING DEGREE OBJECT //
// degree title
Provider.of<AcademicController>(context, listen: false)
.setDegreeTitle(titleController.text);
// degree award
Provider.of<AcademicController>(context, listen: false)
.setDegreeAward(awardController.text);
// HANDLING ACADEMIC YEAR OBJECTS //
// removing academic years to be removed
Provider.of<AcademicController>(context, listen: false)
.removeListOfAcademicYears(academicYearsToBeRemoved);
// saving the changes made within to the academic year form rows
for (AcademicYearFormRow academicYearFormRow in academicYearFormRows) {
academicYearFormRow.saveChanges(context);
}
}
我很困惑,因為在這種方法中,我參考context了 ,但我沒有將 aBuildContext作為引數傳遞給函式。
該方法不是嵌套的,并且與小部件的 build 方法發生在同一級別。
此方法如何訪問 aBuildContext而無需將 a 作為引數?
uj5u.com熱心網友回復:
如果您的_saveChanges方法是 a 的成員StatefulWidget,State則context很可能是State.context。context傳遞給方法的State.build始終是State.context并且在 a 的生命周期內不會改變State。根據檔案State.build
BuildContext引數在此處冗余提供,以便此方法與WidgetBuilder的簽名相匹配。
uj5u.com熱心網友回復:
這取決于該方法是在 aStatelessWidget還是 aStatefulWidget中。我發現對于 a StatefulWidget,在context類中全域可用,而不需要將實體傳遞BuildContext給任何方法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/474153.html
上一篇:使用Mutablelivedata從DialogFragmentColorPicker更改片段的背景顏色不起作用
