상세 컨텐츠

본문 제목

[Flutter] setState() or markNeedsBuild() called during build.

IT/IT 팁

by SINAFLA 2021. 6. 21. 06:00

본문

반응형

에러 메시지

══╡ EXCEPTION CAUGHT BY FOUNDATION LIBRARY ╞════════════════════════════════════════════════════════
The following assertion was thrown while dispatching notifications for EnlargeWin:
setState() or markNeedsBuild() called during build.
This _InheritedProviderScope<EnlargeWin> 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:
  _InheritedProviderScope<EnlargeWin>
The widget which was currently being built when the offending call was made was:
더보기

══╡ EXCEPTION CAUGHT BY FOUNDATION LIBRARY ╞════════════════════════════════════════════════════════ The following assertion was thrown while dispatching notifications for EnlargeWin: setState() or markNeedsBuild() called during build. This _InheritedProviderScope<EnlargeWin> 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: _InheritedProviderScope<EnlargeWin> The widget which was currently being built when the offending call was made was: BodyComponent When the exception was thrown, this was the stack: #0 Element.markNeedsBuild.<anonymous closure> (package:flutter/src/widgets/framework.dart:4211:11) #1 Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:4226:6) #2 _InheritedProviderScopeElement.markNeedsNotifyDependents (package:provider/src/inherited_provider.dart:452:5) #3 ChangeNotifier.notifyListeners (package:flutter/src/foundation/change_notifier.dart:209:21) #4 EnlargeWin.setRealTime (package:kioskapp/providers/body/transfer/detail/VideoProv.dart:147:5) #5 _BodyComponentState.build (package:kioskapp/components/body/BodyComponent.dart:153:19) #6 StatefulElement.build (package:flutter/src/widgets/framework.dart:4663:28) #7 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4546:15) #8 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4719:11) #9 Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5) #10 StatefulElement.update (package:flutter/src/widgets/framework.dart:4751:5) #11 Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15) #12 RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:5566:32) #13 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6001:17) #14 Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15) #15 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5881:14) #16 Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15) #17 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16) #18 Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5) #19 StatelessElement.update (package:flutter/src/widgets/framework.dart:4627:5) #20 Element.updateChild (package:flutter/src/widgets/framework.dart:3245:15) #21 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4571:16) #22 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4719:11) #23 Element.rebuild (package:flutter/src/widgets/framework.dart:4262:5) #24 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2667:33) #25 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:866:20) #26 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:286:5) #27 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1117:15) #28 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1056:9) #29 SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:972:5) #33 _invoke (dart:ui/hooks.dart:253:10) #34 _drawFrame (dart:ui/hooks.dart:211:3) (elided 3 frames from dart:async) The EnlargeWin sending notification was: Instance of 'EnlargeWin' ════════════════════════════════════════════════════════════════════════════════════════════════════ Another exception was thrown: setState() or markNeedsBuild() called during build. Another exception was thrown: setState() or markNeedsBuild() called during build. ════════ Exception caught by foundation library ════════════════════════════════════════════════════ The following assertion was thrown while dispatching notifications for EnlargeWin: setState() or markNeedsBuild() called during build. This _InheritedProviderScope<EnlargeWin> 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: _InheritedProviderScope<EnlargeWin> value: Instance of 'EnlargeWin' listening to value The widget which was currently being built when the offending call was made was: BodyComponent dirty dependencies: [_InheritedProviderScope<CurrentEpn>, _InheritedProviderScope<BodyLoading>, _InheritedProviderScope<MenuIndex>, _InheritedProviderScope<MqttController>] state: _BodyComponentState#fb914 When the exception was thrown, this was the stack: #0 Element.markNeedsBuild.<anonymous closure> (package:flutter/src/widgets/framework.dart:4211:11) #1 Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:4226:6) #2 _InheritedProviderScopeElement.markNeedsNotifyDependents (package:provider/src/inherited_provider.dart:452:5) #3 ChangeNotifier.notifyListeners (package:flutter/src/foundation/change_notifier.dart:209:21) #4 EnlargeWin.setRealTime (package:kioskapp/providers/body/transfer/detail/VideoProv.dart:147:5) ... The EnlargeWin sending notification was: Instance of 'EnlargeWin' ════════════════════════════════════════════════════════════════════════════════════════════════════ ════════ Exception caught by foundation library ════════════════════════════════════════════════════ setState() or markNeedsBuild() called during build. ════════════════════════════════════════════════════════════════════════════════════════════════════ ════════ Exception caught by foundation library ════════════════════════════════════════════════════ setState() or markNeedsBuild() called during build. ════════════════════════════════════════════════════════════════════════════════════════════════════

 


에러 원인

  • Build 되는 부분에 provider의 값 변경되는 함수에 notifyListeners() 메소드가 호출을 했다.
  • 그래서 Build 되는 과정에 꼬여서 저런 에러 메시지가 발생이 된다.

해결

// Test provider
class Test with ChangeNotifiter {
	init() {
    this.a = false;
    this.b = false;
    this.c = false;
  }
}

// Test provider 호출 

class MyMainState extends State<MyMain> {
	
	@override
	Widget build(BuildContext context) {
		Test _test = Provider.of<Test>(context);

		// Test provider 초기화 함수 호출
		_test.init();
		return Container(
			child: Text('Hello World'),
		);
	}	
}

 

반응형

관련글 더보기

댓글 영역