我添加了setState()方法并得到了一個錯誤。終端錯誤(我正在使用flutter run -d chrome --web-hostname localhost --web-port 7357命令,所以它在終端中):
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following assertion was thrown building StreamBuilder<List<BusinessModel>>(dirty, state:
_StreamBuilderBaseState<List<BusinessModel>, AsyncSnapshot<List<BusinessModel>>>#918d6):
setState() or markNeedsBuild() called during build.
This BusinessList widget cannot be marked as needing to build because the framework is already in
the process of building widgets. A widget can be marked as needing to be built during the build
phase only if one of its ancestors is currently building. This exception is allowed because the
framework builds parent widgets before children, which means a dirty descendant will always be
built. Otherwise, the framework might not visit this widget during this build phase.
The widget on which setState() or markNeedsBuild() was called was:
BusinessList
The widget which was currently being built when the offending call was made was:
StreamBuilder<List<BusinessModel>>
The relevant error-causing widget was:
StreamBuilder<List<BusinessModel>>
StreamBuilder:file:///Users/name/development/projects/flutter/project-id/lib/screens/tabs/a_tab_screen.dart:52:
12
When the exception was thrown, this was the stack:
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 266:49 throw_
packages/flutter/src/widgets/framework.dart 4549:11 <fn>
packages/flutter/src/widgets/framework.dart 4563:14 markNeedsBuild
packages/flutter/src/widgets/framework.dart 1134:5 setState
packages/project-id/screens/tabs/a_tab_screen.dart 67:15 <fn>
packages/flutter/src/widgets/async.dart 444:81 build
packages/flutter/src/widgets/async.dart 124:48 build
packages/flutter/src/widgets/framework.dart 4992:27 build
packages/flutter/src/widgets/framework.dart 4878:15 performRebuild
packages/flutter/src/widgets/framework.dart 5050:11 performRebuild
packages/flutter/src/widgets/framework.dart 4604:5 rebuild
packages/flutter/src/widgets/framework.dart 2667:18 buildScope
packages/flutter/src/widgets/binding.dart 882:9 drawFrame
packages/flutter/src/rendering/binding.dart 378:5 [_handlePersistentFrameCallback]
packages/flutter/src/scheduler/binding.dart 1175:15 [_invokeFrameCallback]
packages/flutter/src/scheduler/binding.dart 1104:9 handleDrawFrame
packages/flutter/src/scheduler/binding.dart 1015:5 [_handleDrawFrame]
lib/_engine/engine/platform_dispatcher.dart 1168:13 invoke
lib/_engine/engine/platform_dispatcher.dart 219:5 invokeOnDrawFrame
lib/_engine/engine/initialization.dart 195:45 <fn>
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 334:14 _checkAndCall
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 339:39 dcall
════════════════════════════════════════════════════════════════════════════════════════════════════
我的代碼:
StreamBuilder<List<Model>>(
stream: db.stream(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return …;
}
final List<Model>? data = snapshot.data;
if (data != null) {
for (int i = 0; i < data.length; i ) {
if (data[i].aboutLastUpdated.toDate().month == Timestamp.now().toDate().month) {
setState(() {
final_data.add(data[i]);
});
}
}
return …;
}
return …;
},
);
如果您需要更多資訊,請隨時發表評論!
如何修復此錯誤?我將不勝感激任何幫助。先感謝您!
uj5u.com熱心網友回復:
此錯誤意味著您setState((){})在構建期間呼叫同時執行多個構建。
不要呼叫setState小部件構建。當builder資料準備好時,來自流的已經在重新加載小部件
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/524560.html
標籤:扑镖错误处理设置状态
