如何进行api调用并根据结果返回对话框?

时间:2019-11-11 20:51:07

标签: flutter

我将在这个未来进行API调用

Future<void> _checkAvailabilty() async {
        int updateAvailabilty;
        try {
            final int result = await platform.invokeMethod('getVersionStatus');
            updateAvailabilty = result;

            setState(() {
                _updateAvailabilty=updateAvailabilty;
            });

        } on PlatformException catch (e) {
            print(e);
        }

    }

此方法在我的initState()中被调用,并且效果很好。

我还有另一个异步方法:

_showDialog() async {
        if(_updateAvailabilty ==1){
            return await showDialog(
                context: context,
                builder: (context) {
                    return AlertDialog(
                        title: Text('New Update Available'),
                        actions: <Widget>[
                            FlatButton(
                                onPressed: () {
                                    Navigator.of(context).pop();
                                },
                                child: Text('Close')),
                            FlatButton(
                                onPressed: () {
                                    goToPlaystore();
                                },
                                child: Text('Get Update'),
                            )
                        ],
                    );
                });
        }
    }

使用_checkAvailabilty()中的result作为调用(或不调用)对话框的条件。我不确定将异步方法_showDialog()放在哪里,如果我在initState()中调用它,则什么也不会发生。我应该在哪里调用此方法?我的构建方法是

Widget build(BuildContext context){
        ScreenUtil.instance = ScreenUtil(width: 375, height: 812)..init(context);

        return WillPopScope(
            key: scaffoldKey,
            onWillPop: () => null,
            child: CustomCupertinoTabScaffold(
                tabBar: CustomCupertinoTabBar(
                    border: Border(
                        top: BorderSide(
                            color: Colors.transparent,
                            width: ScreenUtil.getInstance().setWidth(0.0)
                        ),
                    ),
                    backgroundColor: Color(contrastColor2_1),
                    iconSize: 86,
                    activeColor: Color(primaryColor1),
                    items: [
                        /// DASHBOARD
                        BottomNavigationBarItem(
                            icon: ImageIcon(
                                AssetImage("assets/artwork/navigationBarProfile/dashboard/dashBoard.png"),
                                color: (_selectedIndex == 0 ? Color(primaryColor1) : null),
                            ),
                            title: Padding(
                                padding: const EdgeInsets.only(bottom: 5.0),
                                child: RichText(
                                    text: TextSpan(
                                        text: Localize.of(context).trans("iosBottomBar.dashboard"),
                                        style: TextStyle(
                                            fontFamily: montserrat,
                                            fontWeight: FontWeight.w500,
                                            color: Color(contrastColor1),
                                            fontSize: ScreenUtil(allowFontScaling: true).setSp(11),
                                        )
                                    )
                                )
                            )
                        ),

                        /// DISCOVER
                        BottomNavigationBarItem(
                            icon: ImageIcon(
                                AssetImage("assets/artwork/navigationBarProfile/discover/discover.png"),
                                color: (_selectedIndex == 1 ? Color(primaryColor1) : null),
                            ),
                            title: Padding(
                                padding: const EdgeInsets.only(bottom: 5.0),
                                child: RichText(
                                    text: TextSpan(
                                        text: Localize.of(context).trans("iosBottomBar.discover"),
                                        style: TextStyle(
                                            fontFamily: montserrat,
                                            fontWeight: FontWeight.w500,
                                            color: Color(contrastColor1),
                                            fontSize: ScreenUtil(allowFontScaling: true).setSp(11),
                                        )
                                    )
                                )
                            )
                        ),

                        /// PROFILE
                        BottomNavigationBarItem(
                            icon: ImageIcon(
                                AssetImage("assets/artwork/navigationBarProfile/profile/profile.png"),
                                color: (_selectedIndex == 2 ? Color(primaryColor1) : null),
                            ),
                            title: Padding(
                                padding: const EdgeInsets.only(bottom: 2.0),
                                child: RichText(
                                    text: TextSpan(
                                        text: Localize.of(context).trans("iosBottomBar.profile"),
                                        style: TextStyle(
                                            fontFamily: montserrat,
                                            fontWeight: FontWeight.w500,
                                            color: Color(contrastColor1),
                                            fontSize: ScreenUtil(allowFontScaling: true).setSp(11),
                                        )
                                    )
                                )
                            )
                        )
                    ],
                    currentIndex: _selectedIndex,
                    onTap: _onItemTapped,
                ),
                tabBuilder: (lcontext, index){
                    return CupertinoTabView(
                        builder: (lcontext){
                            switch (index){
                                case 0:
                                    return Dashboard(parentContext: context);
                                    break;
                                case 1:
                                    return TopPicks(parentContext: context);
                                    break;
                                default:
                                    return Profile(parentContext: context);
                            }
                        }
                    );
                }
            ),
        );
    }

0 个答案:

没有答案