如何从其他StatefulWidget更新StatefulWidget?

时间:2018-09-07 10:02:45

标签: flutter flutter-layout dart-2

我的屏幕上有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')

屏幕视图如下:

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),
            ],
          ),
        );
  }

}

任何帮助将不胜感激。预先感谢。

3 个答案:

答案 0 :(得分:0)

您可以将Redux的flutter实现用于此类https://pub.dartlang.org/packages/flutter_redux

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

答案 1 :(得分:0)

我已经在https://stackoverflow.com/a/63832366/13439617这里回答了这个问题

您可以使用回调来完成这项工作

答案 2 :(得分:0)

来自推送 stateFul 小部件:

GestureDetector(
            onTap: () async {
             dynamic result = await Navigator.of(context).push(MaterialPageRoute(
                  builder: (context) => CreateUser(
                    usersObj: usersObj,
                  )));
             if (result != null) {
               setState(() {
                 usersObj = result;
               });
             }
            },

来自弹出 stateFul 小部件:

    Navigator.pop(context,usersObj);