我的屏幕有 3 个标签。都有不同的看法。当用户来到屏幕时,我必须点击 API 并更新所有 3 个选项卡的视图。我无法更新选项卡内的视图。获取 API 响应后无法更新不同选项卡的状态。
错误
Error-> NoSuchMethodError: The method 'updateInfo' was called on null.
I/flutter ( 7515): Receiver: null
I/flutter ( 7515): Tried calling: updateInfo(Instance of 'OwnerInfo')
屏幕视图如下:
明细项
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());
});
}
地址视图选项卡
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),
],
),
);
}
}
任何帮助,将不胜感激。提前致谢。