扑从firebase返回空值

时间:2020-09-03 08:12:41

标签: firebase flutter google-cloud-firestore

我正在尝试从Firebase检索值并将其与名称控制器和密码控制器匹配,如果该控制器与Firebase的值匹配,那么我将能够转到下一页。但是在这里,当我按提交firebase返回null。是否有任何方法可以防止null并从firebase中获取原始值

String id;
String password;
FirebaseFirestore firestore = FirebaseFirestore.instance;

//Future<DocumentSnapshot> doc= firestore.collection("proddecAdmin").doc("1").get().then((value) => 
id = value.data()['Id']);

//Future<DocumentSnapshot> doc2= firestore.collection("proddecAdmin").doc().get().then((value) => 
password = value.data()['password']);

Future<DocumentSnapshot> doc3=firestore.collection('proddecAdmin').doc().get().then((DocumentSnapshot 
documentSnapshot) => 
id = documentSnapshot.data()["id"]
);
Future<DocumentSnapshot> doc4=firestore.collection('proddecAdmin').doc().get().then((DocumentSnapshot 
documentSnapshot) => 
password = documentSnapshot.data()["password"]
);

class LoginPage extends StatefulWidget {
@override
State<StatefulWidget> createState() => new _State();
}

class _State extends State<LoginPage> {

 TextEditingController nameController = TextEditingController();
 TextEditingController passwordController = TextEditingController();
 final GlobalKey<FormState> _formKey = GlobalKey();
 String Name1;
 String Name2;

 @override
Widget build(BuildContext context) {
return Scaffold(
    appBar: AppBar(
      title: Text(
        'Admin',
        style: TextStyle(
          fontFamily: 'Ubuntu',
          fontWeight: FontWeight.w700,
          // fontWeight: FontWeight.bold,
          color: Colors.black,
        ),
      ),
      backgroundColor: Colors.white,
      centerTitle: true,
    ),
    body: Center(
        child: Card(
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(26.0),
            ),
            child: Material(
              borderRadius: BorderRadius.circular(26.0),
              elevation: 20.0,
              shadowColor: Color(0x802196F3),
              child: Container(
                  width: 260,
                  height: 300,
                  padding: EdgeInsets.all(16),
                  child: Form(
                    key: _formKey,
                    child: SingleChildScrollView(
                        child: Column(children: <Widget>[
                      TextFormField(
                        controller: nameController,
                        decoration: InputDecoration(labelText: Id),
                        keyboardType: TextInputType.emailAddress,
                        onChanged:(value){
                          setState(() {
                            Name1 = value;
                          });
                        } ,
                        validator: (value) {
                          if (value.isEmpty) {
                            return 'Enter id';
                          }
                          return null;
                        },
                      ),
                      TextFormField(
                        controller: passwordController,
                        decoration: InputDecoration(labelText: Password),
                        obscureText: true,
                        onChanged:(value){
                          setState(() {
                            Name2 = value;
                          });
                        } ,
                        validator: (value) {
                          if (value.isEmpty) {
                            return 'invalid password';
                          }
                          return null;
                        },
                      ),
                      SizedBox(height: 30),
                      RaisedButton(
                        child: Text('Submit'),
                        onPressed: () {
                          print(id);
                          print(password);
                          if (nameController.text == id
                                 &&
                              passwordController.text == password)
                                   {
                            Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) => ProfilePage()),
                            );
                          }

0 个答案:

没有答案