我的顫振應用程式中有一個 Future Builder,它顯示 -
- Error : 如果 json 決議有錯誤
- 資料:如果一切順利
- 裝載機:如果它需要時間
一切正常。未來正在呼叫一個“未來”函式,該函式正在執行一些學生資料的獲取請求,并且“構建器”正在顯示它。我在同一頁面上有一個編輯對話框。我可以通過 put 請求編輯學生資訊。問題是,當我單擊編輯對話框中的表單欄位時,我注意到獲取請求自動發生了大約 10 次。當我保存編輯時,會出現一個確認對話框,表明資料已更新。雖然這種情況再次發生,但獲取請求最多發生 10 次。然后它彈出。因此,服務器上發生了大約 20 個無用的請求。我認為這是因為當我單擊表單欄位時,鍵盤出現并且底層顯示小部件重建,呼叫 api。當資料被編輯時,鍵盤再次回到原來的位置,小部件重建,呼叫 api。我該如何解決這個問題?
如果有幫助,這是代碼:
child: FutureBuilder(
future: APIs().getStudentDetails(),
builder: (context, data) {
if (data.hasError) {
return Padding(
padding: const EdgeInsets.all(8),
child: Center(child: Text("${data.error}")));
} else if (data.hasData) {
var studentData = data.data as List<StudentDetails>;
return Padding(
padding: const EdgeInsets.fromLTRB(0, 15, 0, 0),
child: SingleChildScrollView(
child: SizedBox(
height: MediaQuery.of(context).size.height * 0.9,
child: ListView.builder(
itemCount: studentData.length,
itemBuilder: ((context, index) {
final student = studentData[index];
final id = student.studentId;
final father = student.fatherName;
final mother = student.motherName;
final cg = student.cg;
final cityName = student.city;
final studentName = student.studentName;
return SizedBox(
child: Padding(
padding: const EdgeInsets.all(30.0),
child: SingleChildScrollView(
child: GestureDetector(
onDoubleTap: () {
edit(context, id!, studentName!, father,
mother, cg, cityName!);
},
child: Column(children: [
CustomReadOnlyField(
hintText: id.toString()),
CustomReadOnlyField(hintText: studentName),
CustomReadOnlyField(hintText: father),
CustomReadOnlyField(hintText: mother),
CustomReadOnlyField(
hintText: cg.toString()),
CustomReadOnlyField(hintText: cityName),
]),
),
),
),
);
}),
scrollDirection: Axis.vertical,
),
),
),
);
} else {
return const Center(child: CircularProgressIndicator());
}
},
),
uj5u.com熱心網友回復:
我遵循了這個答案并且它有效。Flutter FutureBuilder 不斷被呼叫 顯然我不得不“懶惰地初始化我的未來”和“在 initState 中初始化我的未來:”
uj5u.com熱心網友回復:
future為like創建一個狀態變數
late final future = APIs().getStudentDetails();
并使用
FutureBuilder(
future: future ,
您可以檢查修復常見的 FutureBuilder 和 StreamBuilder 問題
uj5u.com熱心網友回復:
class _YourWidgetState extends State<YourWidget> with AutomaticKeepAliveClientMixin<YourWidget> {
@override
bool get wantKeepAlive => true;
所以用 AutomaticKeepAliveClientMixin 擴展你的 Widget,這樣 Listview 中的專案就不會被復制
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/520352.html
標籤:扑去
下一篇:在Go回圈中處理特定的運行時錯誤
