- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用flutter编写一个简单的页面,但是当我编译项目并运行调试时,它显示错误:
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)
...
════════════════════════════════════════════════════════════════════════════════════════════════════
这是我调用的代码块:
@override
Widget build(BuildContext context) {
useMemoized(() => DeeplinkHandler.init(context));
useEffect(() => DeeplinkHandler.cancel, const []);
}
明明是在build block中调用的,为什么会报这个错?我该怎么做才能解决这个问题?这是 whole project .我正在使用 Fedora 32 + Android Studio。
最佳答案
Hooks can only be called from the build method of a widget that mix-in
Hooks
.
您的小部件 _MyHomePageState
似乎在扩展 State<> (StatefulWidget) 而不是 HookWidget 或 HookState<>,请查看文档中提供的示例
class _TimeAlive extends Hook<void> {
const _TimeAlive();
@override
_TimeAliveState createState() => _TimeAliveState();
}
class _TimeAliveState extends HookState<void, _TimeAlive> {
DateTime start;
@override
void initHook() {
super.initHook();
start = DateTime.now();
}
@override
void build(BuildContext context) {}
@override
void dispose() {
print(DateTime.now().difference(start));
super.dispose();
}
}
如果你的 widget 是一个有状态的 Widget 改变它看起来像这个(扩展 Hook 和 HookState)
class MyHomePage extends Hook<void>{
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends HookState<void, MyHomePage> {
@override
void initHook() {
super.initHook();
//initialize things here
}
@override
void build(BuildContext context) {
//your hook code here
useMemoized(() => DeeplinkHandler.init(context));
useEffect(() => DeeplinkHandler.cancel, const []);
return Container();
}
@override
void dispose() {
//dispose things here
super.dispose();
}
}
关于flutter - Hooks 只能在 flutter 的 widget 的构建方法中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64122114/
有一条(相对)众所周知的 Perl 公理:“只有 Perl 可以解析 Perl”。我想知道 Perl 6 是否仍然如此? 扩大讨论...考虑到 PyPy 最近的更新,我想到了这个问题。 Perl 独特
这是设置。在上一个问题中,我发现我可以通过子组件中的状态传递对象属性,然后使用 componentDidUpdate 获取该对象属性。在这种情况下,状态和属性都称为到达。 这是基本代码... expo
我运行的是 10.5.2 社区版。我已经标记了 源/主要/资源 作为源目录。我可以右键单击并“编译”某些文件,据我所知,这意味着 IDE 将文件复制到与发送类文件的“com.mydomain.pack
我是一名优秀的程序员,十分优秀!