简体   繁体   English

当子参数更改时,子小部件不更新父小部件

[英]Child widget not updating parent widget when child parameter changes

I have a child stateful widget that isn't updating the parent widget when an update to one of the parameters occurs.我有一个子有状态小部件,当其中一个参数发生更新时,它不会更新父小部件。 The parent widget ( OtherListVIew ) is used to display a list of items on a PaginatedGrid with each item having a checkbox.父小部件 ( OtherListVIew ) 用于在 PaginatedGrid 上显示项目列表,每个项目都有一个复选框。 The problem is that when a user views the page and there are selected items that are retrieved by the _retrieveItems() the changes do not reflect since at this point the ui is already rendered on the screen with the original empty list.问题是,当用户查看页面并且有选定的项目由_retrieveItems()检索时,更改不会反映出来,因为此时 ui 已经在屏幕上呈现原始空列表。 These retrieved values do not reflect on OtherListView ( _selectedItems is then used to display the checked items).这些检索到的值不会反映在OtherListView上( _selectedItems然后用于显示选中的项目)。

In this child widget I have the code在这个子部件中我有代码

List<String> _selectedItems= [];

@override
void didChangeDependencies() {
 super.didChangeDependencies();
 WidgetsBinding.instance.addPostFrameCallback((_) {
  _retrieveItems(); // this function retrieves items and reinitializes _selectedItems
 });
}

@override
Widget build(BuildContext context) {
 return SomeListView(
   key: _listViewKey,
   selectedItems: _selectedItems,
 );
}

SomeListView looks as below SomeListView如下所示

class SomeListView extends OtherListView {

const SomeListView ({super.key, super.selectedItems});

@override
State<SomeListView > createState() => SomeListViewState();
}

class SomeListViewState extends OtherListViewState<SomeListViewState> {
 // override some method here from OtherListView for customization purposes

 @override
 Widget build(BuildContext context) {
   return super.build(context);
 }
}

OtherListView is the parent class that contains the PaginatedGrid that renders the passed _selectedItems to the UI. OtherListView是父级 class,它包含将传递的PaginatedGrid呈现给 UI 的 PaginatedGrid。 Why is it that after the _selectedItems have been refetched they are not reflecting on OtherListView ?为什么在重新获取_selectedItems之后它们没有反映在OtherListView上? Is this an issue with my state management?这是我的 state 管理的问题吗?

I have attempted using didUpdateWidget under OtherListView and indeed I can see the new values under the newWidget but they are not rendered on the UI.我曾尝试在didUpdateWidget下使用OtherListView ,实际上我可以在 newWidget 下看到新值,但它们未呈现在 UI 上。 What I'm I missing?我错过了什么?

@override
void didUpdateWidget(covariant T oldWidget) {
  super.didUpdateWidget(oldWidget);
  if (widget.selectedItems != oldWidget.selectedItems) {
    log.info('OtherListView selected items have been updated.');
  }
}


OtherListView extends StatefulWidget {
  final List<String>? selectedItems;

  const OtherListView({Key? key,this.selectedItems})
     : super(key: key);
  
  // ...
}

honestly i don't understand your code but the problem you are facing the parameter is not updating because with setState it will only rebuild the parameter/state/variables that are in the same screen/class widget where the setState is called so you have to raise your parameter to the parent screen then pass it to the child with constructor and create a constructor in your child for function onTap for example then in the parent screen you use that onTap and called setState inside老实说,我不明白你的代码,但你面临的问题是参数没有更新,因为使用setState它只会重建调用setState的同一screen/class小部件中的parameter/state/variables ,所以你必须将参数提升到父屏幕,然后使用构造函数将其传递给子屏幕,并在子屏幕中为function onTap创建一个构造函数,例如,然后在父屏幕中使用该 onTap 并在内部调用setState

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

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