简体   繁体   English

Dart / Flutter-加载SQLite对象和审核Widget的正确方法

[英]Dart / Flutter - proper way of loading SQLite object and udating Widgets

I've already seen a lot of discussions regarding the subject, but haven't find a proper soultion for my problem. 我已经看过很多有关该主题的讨论,但是还没有找到解决我问题的适当方法。

What I would like to achieve is to pass Process.id from ListView to second widget, then in second widget - load the object from SQLite and update other widgets in Scaffold with Process data. 我想要实现的是将Process.id从ListView传递到第二Process.id部件,然后在第二个小部件中-从SQLite加载对象,并使用Process数据更新Scaffold中的其他小部件。

I've already managed to do that (partially), 'cause I'm struggling with loading data to ListView widget (I have to parse json String to create List and then whenever I'm trying to update ListView.builder using the List - I'm getting blank screen and exceptions). 我已经设法做到了(部分),因为我正在努力将数据加载到ListView小部件中(我必须解析json字符串以创建List,然后每当我尝试使用List更新ListView.builder时,我收到黑屏和异常)。 Commenting out ListView.builder - the widgets works fine... probably I'm putting the code in wrong place ... 注释掉ListView.builder-小部件工作正常...可能我将代码放在错误的位置...

This is how my code looks like: 这是我的代码的样子:

  1. Pass Process.id from ListView in Widget 1 ... 从小部件1中的 ListView传递Process.id ...

     ... return new ListTile( onTap: () { Navigator.push( context, MaterialPageRoute(builder: (context) => ScreenProcessDetails(processId: _newProcesses[index].id)));}, ... 

    to Widget 2 -> DONE: 小部件2- > 完成:

     class ScreenProcessDetails extends StatefulWidget { final int processId; ScreenProcessDetails({Key key, @required this.processId}) : super(key: key); @override State createState() { return new _ScreenProcessDetailsState(); } } 
  2. Loading the Process object from SQLite (using SQFlite) in Widget's 2 ScreenProcessDetails initState() -> DONE, as below: 加载Process从SQLite的(使用SQFlite)在Widget的2对象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 } 

But the filesList is not loaded - whole screen is white and I'm getting list of excepiotns (below). 但是filesList 没有加载 -整个屏幕是白色的,我正在获取excepitons的列表(如下)。 I wonder what I might be doing wrong ? 我想知道我可能做错了什么? I've already tried to use String insted of my Process object with no luck. 我已经尝试过用String插入我的Process对象,但是没有运气。

And one more thing - is this aproach I mean loading object from SQLite in initState() correct ? 还有一件事-这个方法我的意思是从initState() SQLite加载对象正确吗? Beacause I'm seeing one exception: 因为我看到一个例外:

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.

which (as far as I understand) mean that the object was null... but the whole widget is build propely ?! (据我所知)这意味着该对象为null ...但是整个窗口小部件都是正确构建的?

Or should I load SQLite data using Future and then start loading whole widget tree ? 还是应该使用Future加载SQLite数据,然后开始加载整个窗口小部件树? And one more thing - I've tried to use FutureBuilder but it gas been recreated every time I've tried to type text in TextField. 还有一件事-我尝试使用FutureBuilder,但是每次尝试在TextField中键入文本时都会重新创建它。

Exception lsit: 异常lsit:

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

ANY HELP MUCH APPRECIATED ! 允许的任何帮助! THANK YOU IN ADVANCE! 先感谢您! :) :)

发现了问题-列中的ListView必须包装在Flex / Extended小部件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM