Dart / Flutter - 加载 SQLite 对象和更新小部件的正确方法
Dart / Flutter - proper way of loading SQLite object and udating Widgets
我已经看到很多关于这个主题的讨论,但还没有找到适合我的问题的解决方案。
我想要实现的是将 Process.id
从 ListView 传递到第二个小部件,然后在第二个小部件中 - 从 SQLite 加载对象并使用 Process
数据更新脚手架中的其他小部件。
我已经设法做到了(部分),因为我正在努力将数据加载到 ListView 小部件(我必须解析 json 字符串来创建列表,然后每当我尝试使用列表更新 ListView.builder - 我得到空白屏幕和异常)。注释掉 ListView.builder - 小部件工作正常...可能是我把代码放错了地方...
我的代码是这样的:
从 Widget 1 中的 ListView 传递 Process.id
...
...
return new ListTile(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ScreenProcessDetails(processId: _newProcesses[index].id)));},
...
到小部件 2 -> 完成:
class ScreenProcessDetails extends StatefulWidget {
final int processId;
ScreenProcessDetails({Key key, @required this.processId}) : super(key: key);
@override
State createState() {
return new _ScreenProcessDetailsState();
}
}
从 Widget 的 2 中的 SQLite(使用 SQFlite)加载 Process
对象 ScreenProcessDetails
initState()
-> DONE,如下:
class _ScreenProcessDetailsState extends State<ScreenProcessDetails> {
Process process;
Future<Process> getProcess(int id) async {
process = await _repository.getProcess(id);
setState(() {
});
}
@override
void initState() {
super.initState();
getProcess(widget.processId);
}
@override
Widget build(BuildContext context) {
String defaultValue = process.defaultValue;
List<dynamic> filesJson = jsonDecode(process.files);
filesList = filesJson.map((i) => new Files.fromJson(i)).toList();
return new Scaffold(
appBar: new AppBar(title: new Text(process.name)),
body: Builder (builder: (context) => new Container(
child: new Column(
children: <Widget>[
new Text(process.name),
new TextFormField(initialValue: defaultValue,),
new ListView.builder(
physics: const NeverScrollableScrollPhysics(),
itemCount: filesList.length,
itemBuilder: (context, int index) {
return new ListTile(
leading: new Icon(Icons.archive, color: Colors.amber),
title: new Text(filesList[index].name),
subtitle: new Text(filesList[index].guid),
);
})
//skipped rest of code
}
但是 filesList
没有加载 - 整个屏幕都是白色的,我正在获取异常列表(如下)。我想知道我可能做错了什么?我已经尝试使用 String
代替我的 Process
对象,但没有成功。
还有一件事 - 这个方法我的意思是从 initState()
中的 SQLite 加载对象是否正确?因为我看到一个例外:
2019-02-20 13:02:13.023 19208-19288/pl.itelix.documentapp I/flutter: Another exception was thrown: NoSuchMethodError: The getter 'name' was called on null.
这(据我所知)意味着该对象为空...但是整个小部件构建得很好?!
或者我应该使用 Future
加载 SQLite 数据然后开始加载整个小部件树?还有一件事 - 我曾尝试使用 FutureBuilder,但每次我尝试在 TextField 中键入文本时都会重新创建它。
例外情况:
2019-02-20 12:41:07.490 19208-19288/documentapp I/flutter: Another exception was thrown: Vertical viewport was given unbounded height.
2019-02-20 12:41:07.492 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderViewport#627ff NEEDS-LAYOUT NEEDS-PAINT
2019-02-20 12:41:07.493 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderViewport#627ff NEEDS-PAINT
2019-02-20 12:41:07.496 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderIgnorePointer#e5843 relayoutBoundary=up26 NEEDS-PAINT
2019-02-20 12:41:07.500 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderSemanticsAnnotations#bb781 relayoutBoundary=up25 NEEDS-PAINT
2019-02-20 12:41:07.502 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderPointerListener#57754 relayoutBoundary=up24 NEEDS-PAINT
2019-02-20 12:41:07.503 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderSemanticsGestureHandler#54f57 relayoutBoundary=up23 NEEDS-PAINT
2019-02-20 12:41:07.505 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: _RenderScrollSemantics#1370c relayoutBoundary=up22 NEEDS-PAINT
2019-02-20 12:41:07.507 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderRepaintBoundary#c4b4e relayoutBoundary=up21 NEEDS-PAINT
2019-02-20 12:41:07.508 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderCustomPaint#88d67 relayoutBoundary=up20 NEEDS-PAINT
2019-02-20 12:41:07.510 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderRepaintBoundary#b4040 relayoutBoundary=up19 NEEDS-PAINT
2019-02-20 12:41:07.511 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderFlex#09183 relayoutBoundary=up18 NEEDS-PAINT
2019-02-20 12:41:07.513 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderPadding#50678 relayoutBoundary=up17 NEEDS-PAINT
2019-02-20 12:41:07.515 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: _RenderInkFeatures#de7b5 relayoutBoundary=up16 NEEDS-PAINT
2019-02-20 12:41:07.517 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderCustomPaint#05457 relayoutBoundary=up15 NEEDS-PAINT
2019-02-20 12:41:07.519 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderPhysicalShape#2b262 relayoutBoundary=up14 NEEDS-PAINT
2019-02-20 12:41:07.520 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderPadding#5eb3c relayoutBoundary=up13 NEEDS-PAINT
2019-02-20 12:41:07.522 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderSemanticsAnnotations#e3fcc relayoutBoundary=up12 NEEDS-PAINT
2019-02-20 12:41:07.524 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderFlex#8eaef relayoutBoundary=up11 NEEDS-PAINT
2019-02-20 12:41:07.525 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: _RenderSingleChildViewport#c3635 relayoutBoundary=up10 NEEDS-PAINT
2019-02-20 12:41:07.526 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderIgnorePointer#c220a relayoutBoundary=up9 NEEDS-PAINT
2019-02-20 12:41:07.529 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderSemanticsAnnotations#bf875 relayoutBoundary=up8 NEEDS-PAINT
2019-02-20 12:41:07.530 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderPointerListener#789f6 relayoutBoundary=up7 NEEDS-PAINT
2019-02-20 12:41:07.532 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderSemanticsGestureHandler#cabf4 relayoutBoundary=up6 NEEDS-PAINT
2019-02-20 12:41:07.533 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: _RenderScrollSemantics#8ab03 relayoutBoundary=up5 NEEDS-PAINT
2019-02-20 12:41:07.535 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderRepaintBoundary#274a1 relayoutBoundary=up4 NEEDS-PAINT
2019-02-20 12:41:07.536 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderCustomPaint#6174f relayoutBoundary=up3 NEEDS-PAINT
2019-02-20 12:41:07.538 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderRepaintBoundary#a452d relayoutBoundary=up2 NEEDS-PAINT
2019-02-20 12:41:07.539 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderPadding#8c834 relayoutBoundary=up1 NEEDS-PAINT
2019-02-20 12:41:07.543 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderRepaintBoundary#a452d relayoutBoundary=up2 NEEDS-PAINT
非常感谢您的帮助!先感谢您! :)
发现问题 - 列中的 ListView 必须包装在 Flex/Extended 个小部件中。
我已经看到很多关于这个主题的讨论,但还没有找到适合我的问题的解决方案。
我想要实现的是将 Process.id
从 ListView 传递到第二个小部件,然后在第二个小部件中 - 从 SQLite 加载对象并使用 Process
数据更新脚手架中的其他小部件。
我已经设法做到了(部分),因为我正在努力将数据加载到 ListView 小部件(我必须解析 json 字符串来创建列表,然后每当我尝试使用列表更新 ListView.builder - 我得到空白屏幕和异常)。注释掉 ListView.builder - 小部件工作正常...可能是我把代码放错了地方...
我的代码是这样的:
从 Widget 1 中的 ListView 传递
Process.id
...... return new ListTile( onTap: () { Navigator.push( context, MaterialPageRoute(builder: (context) => ScreenProcessDetails(processId: _newProcesses[index].id)));}, ...
到小部件 2 -> 完成:
class ScreenProcessDetails extends StatefulWidget { final int processId; ScreenProcessDetails({Key key, @required this.processId}) : super(key: key); @override State createState() { return new _ScreenProcessDetailsState(); } }
从 Widget 的 2 中的 SQLite(使用 SQFlite)加载
Process
对象ScreenProcessDetails
initState()
-> DONE,如下:class _ScreenProcessDetailsState extends State<ScreenProcessDetails> { Process process; Future<Process> getProcess(int id) async { process = await _repository.getProcess(id); setState(() { }); } @override void initState() { super.initState(); getProcess(widget.processId); } @override Widget build(BuildContext context) { String defaultValue = process.defaultValue; List<dynamic> filesJson = jsonDecode(process.files); filesList = filesJson.map((i) => new Files.fromJson(i)).toList(); return new Scaffold( appBar: new AppBar(title: new Text(process.name)), body: Builder (builder: (context) => new Container( child: new Column( children: <Widget>[ new Text(process.name), new TextFormField(initialValue: defaultValue,), new ListView.builder( physics: const NeverScrollableScrollPhysics(), itemCount: filesList.length, itemBuilder: (context, int index) { return new ListTile( leading: new Icon(Icons.archive, color: Colors.amber), title: new Text(filesList[index].name), subtitle: new Text(filesList[index].guid), ); }) //skipped rest of code }
但是 filesList
没有加载 - 整个屏幕都是白色的,我正在获取异常列表(如下)。我想知道我可能做错了什么?我已经尝试使用 String
代替我的 Process
对象,但没有成功。
还有一件事 - 这个方法我的意思是从 initState()
中的 SQLite 加载对象是否正确?因为我看到一个例外:
2019-02-20 13:02:13.023 19208-19288/pl.itelix.documentapp I/flutter: Another exception was thrown: NoSuchMethodError: The getter 'name' was called on null.
这(据我所知)意味着该对象为空...但是整个小部件构建得很好?!
或者我应该使用 Future
加载 SQLite 数据然后开始加载整个小部件树?还有一件事 - 我曾尝试使用 FutureBuilder,但每次我尝试在 TextField 中键入文本时都会重新创建它。
例外情况:
2019-02-20 12:41:07.490 19208-19288/documentapp I/flutter: Another exception was thrown: Vertical viewport was given unbounded height.
2019-02-20 12:41:07.492 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderViewport#627ff NEEDS-LAYOUT NEEDS-PAINT
2019-02-20 12:41:07.493 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderViewport#627ff NEEDS-PAINT
2019-02-20 12:41:07.496 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderIgnorePointer#e5843 relayoutBoundary=up26 NEEDS-PAINT
2019-02-20 12:41:07.500 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderSemanticsAnnotations#bb781 relayoutBoundary=up25 NEEDS-PAINT
2019-02-20 12:41:07.502 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderPointerListener#57754 relayoutBoundary=up24 NEEDS-PAINT
2019-02-20 12:41:07.503 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderSemanticsGestureHandler#54f57 relayoutBoundary=up23 NEEDS-PAINT
2019-02-20 12:41:07.505 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: _RenderScrollSemantics#1370c relayoutBoundary=up22 NEEDS-PAINT
2019-02-20 12:41:07.507 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderRepaintBoundary#c4b4e relayoutBoundary=up21 NEEDS-PAINT
2019-02-20 12:41:07.508 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderCustomPaint#88d67 relayoutBoundary=up20 NEEDS-PAINT
2019-02-20 12:41:07.510 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderRepaintBoundary#b4040 relayoutBoundary=up19 NEEDS-PAINT
2019-02-20 12:41:07.511 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderFlex#09183 relayoutBoundary=up18 NEEDS-PAINT
2019-02-20 12:41:07.513 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderPadding#50678 relayoutBoundary=up17 NEEDS-PAINT
2019-02-20 12:41:07.515 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: _RenderInkFeatures#de7b5 relayoutBoundary=up16 NEEDS-PAINT
2019-02-20 12:41:07.517 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderCustomPaint#05457 relayoutBoundary=up15 NEEDS-PAINT
2019-02-20 12:41:07.519 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderPhysicalShape#2b262 relayoutBoundary=up14 NEEDS-PAINT
2019-02-20 12:41:07.520 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderPadding#5eb3c relayoutBoundary=up13 NEEDS-PAINT
2019-02-20 12:41:07.522 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderSemanticsAnnotations#e3fcc relayoutBoundary=up12 NEEDS-PAINT
2019-02-20 12:41:07.524 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderFlex#8eaef relayoutBoundary=up11 NEEDS-PAINT
2019-02-20 12:41:07.525 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: _RenderSingleChildViewport#c3635 relayoutBoundary=up10 NEEDS-PAINT
2019-02-20 12:41:07.526 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderIgnorePointer#c220a relayoutBoundary=up9 NEEDS-PAINT
2019-02-20 12:41:07.529 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderSemanticsAnnotations#bf875 relayoutBoundary=up8 NEEDS-PAINT
2019-02-20 12:41:07.530 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderPointerListener#789f6 relayoutBoundary=up7 NEEDS-PAINT
2019-02-20 12:41:07.532 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderSemanticsGestureHandler#cabf4 relayoutBoundary=up6 NEEDS-PAINT
2019-02-20 12:41:07.533 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: _RenderScrollSemantics#8ab03 relayoutBoundary=up5 NEEDS-PAINT
2019-02-20 12:41:07.535 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderRepaintBoundary#274a1 relayoutBoundary=up4 NEEDS-PAINT
2019-02-20 12:41:07.536 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderCustomPaint#6174f relayoutBoundary=up3 NEEDS-PAINT
2019-02-20 12:41:07.538 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderRepaintBoundary#a452d relayoutBoundary=up2 NEEDS-PAINT
2019-02-20 12:41:07.539 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderPadding#8c834 relayoutBoundary=up1 NEEDS-PAINT
2019-02-20 12:41:07.543 19208-19288/documentapp I/flutter: Another exception was thrown: RenderBox was not laid out: RenderRepaintBoundary#a452d relayoutBoundary=up2 NEEDS-PAINT
非常感谢您的帮助!先感谢您! :)
发现问题 - 列中的 ListView 必须包装在 Flex/Extended 个小部件中。