Dart / Flutter - правильный способ загрузки объекта SQLite и обновления виджетов
Я уже видел много дискуссий по этому вопросу, но не нашел подходящего решения для моей проблемы.
То, что я хотел бы достичь, это пройти Process.id
из ListView во второй виджет, затем во второй виджет - загрузите объект из SQLite и обновите другие виджеты в Scaffold с помощью Process
данные.
Мне уже удалось сделать это (частично), потому что я борюсь с загрузкой данных в виджет ListView (мне нужно проанализировать строку json для создания List, а затем всякий раз, когда я пытаюсь обновить ListView.builder, используя List - Я получаю пустой экран и исключения). Закомментирование ListView.builder - виджеты работают нормально... возможно, я помещаю код в неправильное место...
Вот как выглядит мой код:
Проходить
Process.id
из ListView в виджете 1...... 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(); } }
Загрузка
Process
объект из SQLite (используя SQFlite) в виджете 2ScreenProcessDetails
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
объект без удачи.
И еще одна вещь - этот подход я имею в виду загрузку объекта из SQLite в initState()
правильный? Потому что я вижу одно исключение:
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.
что (насколько я понимаю) означает, что объект был нулевым... но весь виджет построен правильно?!
Или я должен загрузить данные SQLite, используя Future
а затем начать загрузку всего дерева виджетов? И еще одна вещь - я пытался использовать 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 ответ
Нашел проблему - ListView в столбцах должен быть обернут в виджеты Flex/Extended.