我已經復制了StatefulWidget的檔案來創建此代碼:
class Note extends StatefulWidget
{
final String initialValue;
const Note({Key? key, this.initialValue = ""}) : super(key: key);
@override
State<StatefulWidget> createState()
{
return NoteState();
}
}
class NoteState extends State<Note>
{
String value = "";
NoteState()
{
value = widget.initialValue;
}
@override
Widget build(BuildContext context)
{
return Text(value);
}
}
使用包含以下內容的主應用程式腳手架:
body: const Center
(
child: note_widget.Note(key: Key('key1'), initialValue: 'test text'),
),
運行它時,我在下面得到這個回圈輸出,標簽從不顯示。請問我做錯了什么?
[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Cannot hit test a render box that has never been laid out.
The hitTest() method was called on this RenderBox: RenderErrorBox#d7972 NEEDS-LAYOUT NEEDS-PAINT:
creator: ErrorWidget-[#1866a] ← _BodyBuilder ← MediaQuery ← LayoutId-[<_ScaffoldSlot.body>] ← CustomMultiChildLayout ← AnimatedBuilder ← DefaultTextStyle ← AnimatedDefaultTextStyle ← _InkFeatures-[GlobalKey#583c9 ink renderer] ← NotificationListener<LayoutChangedNotification> ← PhysicalModel ← AnimatedPhysicalModel ← ?
parentData: offset=Offset(0.0, 0.0); id=_ScaffoldSlot.body
constraints: MISSING
size: MISSING
Unfortunately, this object's geometry is not known at this time, probably because it has never been laid out. This means it cannot be accurately hit-tested.
If you are trying to perform a hit test during the layout phase itself, make sure you only hit test nodes that have completed layout (e.g. the node's children, after their layout() method has been called).
#0 RenderBox.hitTest.<anonymous closure> (package:flutter/src/rendering/box.dart:2380:11)
(如果我使用child: Text('Hello World')該應用程式運行良好)
更新:原始錯誤發生在上面列出的錯誤頁面之前,是:
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following _CastError was thrown building _BodyBuilder:
Null check operator used on a null value
The relevant error-causing widget was:
Scaffold Scaffold:file:///c:/data/tfjournal/lib/screens/home_screen.dart:11:10
When the exception was thrown, this was the stack:
#0 State.widget (package:flutter/src/widgets/framework.dart:883:26)
#1 new NoteState (package:tfjournal/widgets/note.dart:22:11)
#2 Note.createState (package:tfjournal/widgets/note.dart:12:10)
#3 new StatefulElement (package:flutter/src/widgets/framework.dart:4754:25)
uj5u.com熱心網友回復:
要更新 UI 背景關系,您需要在無遺囑中進行初始化。否則,您會得到空值例外Null check operator used on a null value
改變
NoteState()
{
value = widget.initialValue;
}
到
@override
void initState() {
super.initState();
value = widget.initialValue;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/372203.html
標籤:扑
