I am writing a simple page using flutter, but when I compile the project and run debug, it shows error:
Launching lib/main.dart on sdk gphone x86 arm in debug mode...
Running Gradle task 'assembleDebug'...
✓ Built build/app/outputs/flutter-apk/app-debug.apk.
Installing build/app/outputs/flutter-apk/app.apk...
Waiting for sdk gphone x86 arm to report its views...
Debug service listening on ws://127.0.0.1:42239/B45QwfJKV30=/ws
Syncing files to device sdk gphone x86 arm...
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following assertion was thrown building MyHomePage(dirty, state: _MyHomePageState#97d08):
Hooks can only be called from the build method of a widget that mix-in `Hooks`.
Hooks should only be called within the build method of a widget.
Calling them outside of build method leads to an unstable state and is therefore prohibited.
'package:flutter_hooks/src/framework.dart':
Failed assertion: line 142 pos 12: 'HookElement._currentHookElement != null'
The relevant error-causing widget was:
MyHomePage file:///home/dolphin/AndroidStudioProjects/Cruise/lib/src/widgets/MyApp.dart:27:13
When the exception was thrown, this was the stack:
#2 Hook.use (package:flutter_hooks/src/framework.dart:142:12)
#3 use (package:flutter_hooks/src/framework.dart:19:32)
#4 useMemoized (package:flutter_hooks/src/primitives.dart:11:10)
#5 _MyHomePageState.build (package:Cruise/src/states/_MyHomePageState.dart:121:5)
#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 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4525:5)
#11 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4710:11)
#12 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4520:5)
... Normal element mounting (132 frames)
#144 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3490:14)
#145 MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5991:32)
... Normal element mounting (273 frames)
#418 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3490:14)
#419 Element.updateChild (package:flutter/src/widgets/framework.dart:3258:18)
#420 RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1174:16)
#421 RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1145:5)
#422 RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:1087:17)
#423 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2620:19)
#424 RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1086:13)
#425 WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:927:7)
#426 WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:908:7)
(elided 13 frames from class _AssertionError, class _RawReceivePortImpl, class _Timer, dart:async, and dart:async-patch)
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following assertion was thrown building MyHomePage(dirty, state: _MyHomePageState#97d08):
Hooks can only be called from the build method of a widget that mix-in `Hooks`.
Hooks should only be called within the build method of a widget.
Calling them outside of build method leads to an unstable state and is therefore prohibited.
'package:flutter_hooks/src/framework.dart':
Failed assertion: line 142 pos 12: 'HookElement._currentHookElement != null'
The relevant error-causing widget was:
MyHomePage file:///home/dolphin/AndroidStudioProjects/Cruise/lib/src/widgets/MyApp.dart:27:13
When the exception was thrown, this was the stack:
#2 Hook.use (package:flutter_hooks/src/framework.dart:142:12)
#3 use (package:flutter_hooks/src/framework.dart:19:32)
#4 useMemoized (package:flutter_hooks/src/primitives.dart:11:10)
#5 _MyHomePageState.build (package:Cruise/src/states/_MyHomePageState.dart:121:5)
#6 StatefulElement.build (package:flutter/src/widgets/framework.dart:4663:28)
...
════════════════════════════════════════════════════════════════════════════════════════════════════
this is the code block I am called:
@override
Widget build(BuildContext context) {
useMemoized(() => DeeplinkHandler.init(context));
useEffect(() => DeeplinkHandler.cancel, const []);
}
Obviously I am called in build block, why give this error? what should I do to fix this? this is the whole project. I am using Fedora 32 + Android Studio.