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

时间:2019-02-20 12:06:58

标签: sqlite dart flutter widget sqflite

我已经看过很多关于该主题的讨论,但是还没有找到解决我问题的适当方法。

我想要实现的是将Process.id从ListView传递到第二个小部件,然后在第二个小部件中-从SQLite加载对象,并使用Process数据更新Scaffold中的其他小部件。

我已经(部分地)做到了这一点,因为我正努力将数据加载到ListView小部件中(我必须解析json字符串以创建List,然后每当我尝试使用以下方法更新ListView.builder时列表-我将出现空白屏幕和异常)。注释掉ListView.builder-小部件可以正常工作...可能我将代码放在错误的位置...

这是我的代码的样子:

  1. 小部件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();
        }
    }
    
  2. 小部件的2 Process ScreenProcessDetails-> DONE中,从SQLite(使用SQFlite)加载initState()对象,如下所示:

    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.

(据我所知)这意味着该对象为null ...但是整个窗口小部件都是正确构建的?!

还是应该使用Future加载SQLite数据,然后开始加载整个窗口小部件树?还有一件事-我尝试使用FutureBuilder,但是每次尝试在TextField中键入文本时都会重新创建它。

异常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

有任何帮助!先感谢您! :)

1 个答案:

答案 0 :(得分:0)

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