gpt4 book ai didi

flutter - 如何从其他 StatefulWidget 更新 StatefulWidget?

转载 作者:IT王子 更新时间:2023-10-29 07:06:13 26 4
gpt4 key购买 nike

我的屏幕上有 3 个标签。都有不同的看法。当用户来到屏幕上时,我必须点击 API 并更新所有 3 个选项卡的 View 。我无法更新选项卡内的 View 。获取 API 响应后无法更新不同选项卡的状态。

Error

Error-> NoSuchMethodError: The method 'updateInfo' was called on null.
I/flutter ( 7515): Receiver: null
I/flutter ( 7515): Tried calling: updateInfo(Instance of 'OwnerInfo')

屏幕 View 如下:

enter image description here

DetailItem

class DetailItem extends StatefulWidget {
PropertyItem _propertyObj;
OwnerInfo _ownerInfo;
Review _reviewInfo;

@override
_DetailItemState createState() => _DetailItemState();
}

class _DetailItemState extends State<DetailItem> with TickerProviderStateMixin {
ScrollController _scrollController = new ScrollController();
double _appBarHeight = 0.0;
TabController _tabController;

final GlobalKey<AddressViewTabState> key =
new GlobalKey<AddressViewTabState>();

//Tabs used in Detail Screen
DetailViewTab _detailViewTab;
AddressViewTab _addressViewTab;
ReviewTab _reviewTab;

@override
void initState() {
_tabController = TabController(length: 3, vsync: this, initialIndex: 0); // initialize tab controller
//Initialize Tabs
_detailViewTab = DetailViewTab(widget._propertyObj);
_addressViewTab = AddressViewTab(key: key, propertyDetailModel: widget._ownerInfo);
_reviewTab = ReviewTab(widget._propertyObj);

_hitDetailAPI(); //init state Hit API
super.initState();
}

//Detail API
void _hitDetailAPI() async {
Future<PropertyDetailModel> obj = APIHandler()
.getPropertyDetail(widget._propertyObj.getRoomId.toString());
obj.then((response) {
if (!mounted) {
return;
}
//update data in set state
setState(() {
if (response != null && response.getPropertyItem != null) {
widget._propertyObj = response.getPropertyItem; // update data to property object the 1st tab(Detail)
widget._ownerInfo = response.getOwnerInfo; // update data to owner info object the second tab (Address)
key.currentState.updateInfo(widget._ownerInfo); // Tried to update the address tab view by calling through key current state.
}
});
}).catchError((error) {
print("Error-> " + error.toString());
});
}

AddressViewTab

class AddressViewTab extends StatefulWidget {
OwnerInfo ownerInfo = OwnerInfo();
AddressViewTab({ Key key , @required this.ownerInfo}) : super(key: key);

@override
AddressViewTabState createState() => AddressViewTabState();

}

class AddressViewTabState extends State<AddressViewTab>{

updateInfo(OwnerInfo ownerInfo){
print("Update method called");
if (!mounted) {
return;
}
setState(() {
widget.ownerInfo=ownerInfo; // update state
});
}

@override
Widget build(BuildContext context) {
return Container(
alignment: Alignment.center,
child: ListView(
children: <Widget>[
getOwnerInfoCard(widget.ownerInfo),
],
),
);
}

}

如有任何帮助,我们将不胜感激。提前致谢。

最佳答案

您可以使用 Redux 的 flutter 实现来处理这样的事情 https://pub.dartlang.org/packages/flutter_redux为此。

简而言之,这可以使“任何”小部件在某些数据更改时自行更新。

关于flutter - 如何从其他 StatefulWidget 更新 StatefulWidget?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52220156/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com