Skip to content

FutureBuilder fire #11956

Closed
Closed

Description

@arcas0803

Future builder fires every time a TextInputField is touched

    return new FutureBuilder<FirebaseUser>(future: _initAsync(),
      builder: (BuildContext context, AsyncSnapshot<FirebaseUser> snapshot) {
        switch (snapshot.connectionState) {
          case ConnectionState.none:
            return new Text('Press button to start');
          case ConnectionState.waiting:
            return new Text('Awaiting result...');
          default:
            if (snapshot.hasError)
              return new Text('Error: ${snapshot.error}');
            else
              if(snapshot.data == null){
                return new Login();
              }else{
                return new Home();
              }

        }
      },

In the login widget when the user taps into a texfield the future builder fires which does not have sense.

class _LoginState extends State<Login> {

  //Auth

  final localAuth = new LocalAuthentication();
  final firebaseAuth = FirebaseAuth.instance;

  //SharePreferences

  final String userSharePreference = "User";
  final String passwordSharePreference = "Password";

  //Log in

  String user;
  String password;

  _logIn() async {

    firebaseAuth.signInWithEmailAndPassword(email: user, password: password)
        .then((FirebaseUser user){
          Navigator.of(context).push(new MaterialPageRoute<Null>(builder: (BuildContext context) {
                return new Home();
              }));
        })
        .catchError((e){
          Scaffold.of(context).showSnackBar(new SnackBar(content: new Text(e.toString())));
    });
  }

  _logInFingerprint() async{

    SharedPreferences prefs = await SharedPreferences.getInstance();
    await localAuth.authenticateWithBiometrics(localizedReason: "FINGERPRINT")
      .then((bool result){

        setState((){
          user = prefs.getString(userSharePreference);
          password = prefs.getString(passwordSharePreference);
        });

        firebaseAuth.signInWithEmailAndPassword(email: user, password: password)
            .then((FirebaseUser user){
              Navigator.of(context).push(
                  new MaterialPageRoute<Null>(builder: (BuildContext context) {
                    return new Home();
                  }));
            })
            .catchError((e){
              Scaffold.of(context).showSnackBar(new SnackBar(content: new Text(e.toString())));
            });
      }).catchError((e){
      Scaffold.of(context).showSnackBar(new SnackBar(content: new Text(e.toString())));
    });
  }


  _showPassword() {}

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        body: new Container(
            color: Colors.indigo,
            child: new Center(
                child: new Padding(
                    padding: new EdgeInsets.all(10.0),
                    child: new Card(
                      child: new Padding(
                        padding:
                            new EdgeInsets.fromLTRB(15.0, 10.0, 15.0, 10.0),
                        child: new Column(
                          mainAxisSize: MainAxisSize.min,
                          children: <Widget>[
                            new FlutterLogo(
                              size: 100.0,
                            ),
                            new TextFormField(
                              decoration: new InputDecoration(
                                hintText: "Insert user",
                                labelText: "User",
                                helperText: "Insert a valid user",
                              ),
                            ),
                            new TextFormField(
                              decoration: new InputDecoration(
                                  hintText: "Insert Password",
                                  labelText: "Password",
                                  helperText: "Insert a valid Password"),
                              obscureText: true,
                            ),
                            new ButtonTheme.bar(
                              // make buttons use the appropriate styles for cards
                              child: new ButtonBar(
                                children: <Widget>[
                                  new FlatButton(
                                    child: const Text('FINGERPRINT'),
                                    onPressed: _logInFingerprint,
                                    color: Colors.teal,
                                    textColor: Colors.white,
                                  ),
                                  new FlatButton(
                                    child: const Text('LOG IN'),
                                    onPressed: _logIn,
                                    color: Colors.blue,
                                    textColor: Colors.white,
                                  ),
                                ],
                              ),
                            ),
                          ],
                        ),
                      ),
                    )))));
  }
}```

Activity

  1. Hixie commented on May 18, 2018

    @Hixie
    Contributor

    I don't understand the sample code above nor the issue being filed here (e.g. how do these code snippets relate?). Can you elaborate?

  2. cfkloss commented on Jun 10, 2018

    @cfkloss

    It seems related to #11426

  3. zoechi commented on Aug 26, 2018

    @zoechi
    Contributor

    Closing as dup of #11426
    Thanks @cfkloss

  4. github-actions commented on Sep 2, 2021

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  5. locked as resolved and limited conversation to collaborators on Sep 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions