У меня экран с 3 вкладками. У всех разные взгляды. Когда пользователь выходит на экран, я должен нажать API и обновить представления для всех 3 вкладок. Я не могу обновить вид внутри вкладок.
Невозможно обновить состояния разных вкладок после получения ответа 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')
Вид экрана похож на:
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),
],
),
);
}
}
Любая помощь будет оценена. Заранее спасибо.