在Flutter的TextFormFields中使用FutureBuilder

时间:2020-08-22 00:57:47

标签: firebase flutter google-cloud-firestore

我创建了一个名为_buildEmailFormField()的方法,该方法在构建方法中调用。我正在尝试显示在开设帐户时使用的用户电子邮件,以便他们可以在存档中更新它。电子邮件必须始终存在,因为没有空间可以容纳空的电子邮件数据。从firebase读取电子邮件时,应将电子邮件值作为快照数据返回。

  _buildEmailField() {
    FutureBuilder<String>(
      future: AuthProvider.of(context).auth.getCurrentUserEmail(),
      builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
        return TextFormField(
                  initialValue: snapshot.data,
                  keyboardType: TextInputType.emailAddress,
                  textAlign: TextAlign.left,
                  onSaved: (value) => snapshot.data = value;                   
        );
      },
    );
  }

但是,它不起作用。相反,我收到了这些错误消息

在构建ProfileFormScreen(dirty, 依赖项:[AuthProvider],状态:_ProfileFormScreenState#0fcf0): 列的子级不得包含任何空值,而应包含空值 被发现在索引18

我的AuthProvider类

class AuthProvider extends InheritedWidget {
  AuthProvider({
    Key key,
    @required this.auth,
    @required Widget child,
  })  : assert(auth != null),
        assert(child != null),
        super(key: key, child: child);

  final BaseAuth auth;

  @override
  bool updateShouldNotify(AuthProvider old) => auth != old.auth;

  //this static class allows us to access AuthProvider anywhere in our code using AuthProvider.of(context).auth
  static AuthProvider of(BuildContext context) {
    return context.dependOnInheritedWidgetOfExactType<AuthProvider>();
  }
}

我的Auth类处理我的身份验证

abstract class BaseAuth {
  Stream<User> get user;
  Stream<String> get onAuthStateChanged;
  Future<String> handleSignIn(String email, String password);
  Future<String> handleSignUp(String email, String password);
  Future signOut();
  Future<String> currentUser();
  Future<String> getCurrentUserEmail();
}

class Auth implements BaseAuth {
  final _auth = FirebaseAuth.instance;

  //create a new user object based on FirebaseUser
  User _userFromFirebaseUser(FirebaseUser user) {
    return user != null ? User(uid: user.uid) : null;
  }

  //auth change user stream
  Stream<User> get user {
    return _auth.onAuthStateChanged.map(_userFromFirebaseUser);
  }

  @override
  Stream<String> get onAuthStateChanged {
    return _auth.onAuthStateChanged.map((user) => user?.uid);
  }

  //sign in with email and password
  Future<String> handleSignIn(String email, String password) async {
    AuthResult result = await FirebaseAuth.instance
        .signInWithEmailAndPassword(email: email, password: password)
        .catchError((error) => print(error));

    if (result != null) {
      FirebaseUser user = result.user;
      if (user != null) {
        print('From handleSignIn, Log in: ${user.email}');
      }
      return user.uid;
    }
  }

  //sign up with email and password
  Future<String> handleSignUp(String email, String password) async {
    AuthResult result = await FirebaseAuth.instance
        .createUserWithEmailAndPassword(email: email, password: password)
        .catchError((error) => print(error));
    FirebaseUser user = result.user;
    return user.uid;
  }

  //sign out user
  Future signOut() async {
    try {
      return await _auth.signOut();
    } catch (e) {
      print(e.toString());
      return null;
    }
  }

  Future<String> currentUser() async {
    FirebaseUser user = await _auth.currentUser();
    return user.uid;
  }

  Future<String> getCurrentUserEmail() async {
    FirebaseUser user = await _auth.currentUser();
    final String email = user.email.toString();
    print(email);
    return email;
  }
}

在这里我可能会忽略什么?

0 个答案:

没有答案