为什么Facebook在扑朔迷离地登录数据后,futureBuilder回调不调用? [英] why futureBuilder callback is not calling after facebook logged in data in flutter?

查看:127
本文介绍了为什么Facebook在扑朔迷离地登录数据后,futureBuilder回调不调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,所以如果我错了,请告诉我.在我的应用程序中,我必须使用Facebook登录,所以我在使用的以下代码中使用了RawMaterialButtononPressed函数

I am newbie in flutter so please let me know if i am wrong.In my application i have to logged in using facebook so i took a RawMaterialButton and in onPressed function in used following code

child: RawMaterialButton(
                    onPressed: () {
                      FutureBuilder<String>(
                        future:
                            _login(), // function where you call your api
                        builder: (BuildContext context,
                            AsyncSnapshot<String> snapshot) {
                          // AsyncSnapshot<Your object type>
                          print(snapshot);
                          if (snapshot.connectionState ==
                              ConnectionState.done) {
                            print('${snapshot.data}');
                          }
                          if (snapshot.connectionState ==
                              ConnectionState.waiting) {
                            return Center(
                                child: Text('Please wait its loading...'));
                          } else {
                            if (snapshot.hasError)
                              return Center(
                                  child: Text('Error: ${snapshot.error}'));
                            else
                              return Center(
                                  child: new Text(
                                      '${snapshot.data}')); // snapshot.data  :- get your object which is pass from your downloadData() function
                          }
                        },
                      );
                      return CircularProgressIndicator();
                    },

当我按下按钮调用其方法时,我正在获取数据,但是获取这些数据后,将来的构建将不会更新或在Text中显示该数据.这是我的数据提取方法.

when i press the button its calling the method and i am getting data but after getting those data future building is not updating or showing that data in a Text.here is my data fetch method.

  Future<String> _login() async {
final result = await FacebookAuth.instance.login();
var tokenString;
switch (result.status) {
  case FacebookAuthLoginResponse.ok:
    _printCredentials(result);
    // get the user data
    // final userData = await FacebookAuth.instance.getUserData();
    tokenString = result.accessToken.token;
    break;
  case FacebookAuthLoginResponse.cancelled:
    print("login cancelled");
    tokenString = "User cancelled";
    break;
  default:
    tokenString = "Login failed";
}
return tokenString;
}

出于调试目的,我也创建了断点,但是它没有直接指向asynchronous snapshot条件状态检查.我无法找到原因.我还检查了以下堆栈问题.

For debug purpose i made break point also but its not directing to the asynchronous snapshot condition state checking.I could not find out the reasons. I also check following stack question also.

为什么FutureBuilder无法在Flutter中获得呼叫? Flutter FutureBuilder未更新

推荐答案

好吧,在onPressed函数中,您正在声明FutureBuilder,这肯定是错误的. FutureBuilder允许您对函数或方法进行异步请求,以使用响应数据来构建窗口小部件或其他组件,具体取决于快照值.当您第一次运行窗口小部件或在StatefulWidget中使用SetState({})时,会发生这种情况.但是,您正在将视图代码与功能代码结合在一起.这是实现FutureBuilder的方法:

Well, in your onPressed function you are declarating a FutureBuilder and it's definitly wrong. FutureBuilder allows you to make an asynchronous request for a function or method to use the response data to build a widget or another depending of the snapshot value. This happens when you run the widget for the first time or use SetState({}) in a StatefulWidget. But you are joining view code with functional code. This is the way to implement a FutureBuilder:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: _login(),
      builder: (context, snapshot) {
        print(snapshot);
        if (snapshot.connectionState == ConnectionState.done) {
          print('${snapshot.data}');
        }
        if (snapshot.connectionState == ConnectionState.waiting) {
          return Center(child: Text('Please wait its loading...'));
          // Or return CircularProgressIndicator();
        } else {
          if (snapshot.hasError)
            return Center(child: Text('Error: ${snapshot.error}'));
          else
            return Center(
                child: new Text(
                    '${snapshot.data}'));
        }
      },
    );
    // if you return something here, it's dead code
  }

但是我建议您以这种方式实现FutureBuilder:

But i would suggest you to implement FutureBuilder in this way:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      child: FutureBuilder(
        future: _login(),
        builder: (context, snapshot) {
          if (snapshot.hasError) // if it has error
            return Text('Error: ${snapshot.error}');
          if (!snapshot.hasData) {
            // if it's loading
            return Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text('Please wait its loading...'),
                Padding(
                  padding: EdgeInsets.only(top: 20),
                  child: Container(
                    height: 35,
                    width: 35,
                    child: CircularProgressIndicator(),
                  ),
                ),
              ],
            );
          } else {
            // if everything success
            print(snapshot.data);
            return Text('${snapshot.data}');
          }
        },
      ),
    );
  }

最后,我想您想要的是使用提供者在登录页面的小部件之间创建通信方式,因为您想通过按一个按钮来更改页面/视图小部件的状态,您可以使用StatefulWidget,但是您可以应该更好地参见本教程. 此处.

Finally, what i think you want is use a Provider to create a comunication way between widgets into your login page because you want to change the State of the page/view widget by pressing a button, u could use a StatefulWidget but u should see this tutorial better. and here is the provider documentation.

Pdst:但是,如果您不想实现提供程序,则可以在按钮中发出_login()请求,例如:

Pdst: But, if u don't want to implement provider you could make the _login() request in the button like:

onPressed: () async {
  String response = await _login();
  Navigator.push(context, MaterialPageRoute(builder: (context) => ResponseWidget(response)));
}

使用navigator.push(),您可以更改屏幕上的视图/页面/小部件,并显示一个新的小部件,该小部件可能显示一些依赖于响应值的东西.

With navigator.push() you can change the view/page/widget that u have in your screen and show a new widget that could show something deppending the response value.

这篇关于为什么Facebook在扑朔迷离地登录数据后,futureBuilder回调不调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆