why futureBuilder callback is not calling after facebook logged in data in flutter?

前端 未结 1 1132
清酒与你
清酒与你 2021-01-28 14:10

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

1条回答
  •  孤独总比滥情好
    2021-01-28 14:49

    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
      }
    

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

    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: 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)));
    }
    

    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.

    0 讨论(0)
提交回复
热议问题