How to use one field of firebase to login

≡放荡痞女 提交于 2021-02-15 07:39:16

问题


I write the following code and when I click the next Log in button I get the following error when clicking the login button.

type 'Future' is not a subtype of type 'String'

I want to compare the "Phone" with the user input phone number. How can I verify that which the user enters Phone is the same which in firestore or not?

my code is:

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:fmiantrader/splash.dart';
import 'componenets.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
class Login extends StatefulWidget {
  static String id ='Login Screen';
  @override
  _LoginState createState() => _LoginState();
}

class _LoginState extends State<Login> {




  TextEditingController  _name = TextEditingController();
  TextEditingController  _phone = TextEditingController();


  final _form =GlobalKey<FormState>();
  final _formforphone =GlobalKey<FormState>();
  final _firestore=Firestore.instance;

  getData() async {
    List<String> store=[];
    String name;
    await for(var snapshot in _firestore.collection('users').snapshots()){
      for(var phon in snapshot.documents){
        name=(phon.data['Phone']);
        store=[name];
        print(store);

      }
    }
    return store;
  }



  String _validateMobile(String value) {
    String pattern = r'(^(?:[+0]9)?[0-9]{11}$)';
    RegExp regExp = new RegExp(pattern);
    if (value.length == 0) {
      return '*Required';
    } else if (!regExp.hasMatch(value)) {
      return 'Invalid - Put last 11 digits only';
    }
    return null;
  }


  Widget _textFieldForName(){
    return Form(

      key: _form,
      child: Column(
        children: <Widget>[
          TextFormField(
              controller: _name,
              decoration: InputDecoration(
                contentPadding: EdgeInsets.symmetric(vertical: 10.0,horizontal: 20.0),
                border: OutlineInputBorder(
                  borderSide: BorderSide(color:Colors.blueAccent,width: 1.0),
                  borderRadius: BorderRadius.circular(32.0),
                ),
                enabledBorder: OutlineInputBorder(
                    borderSide: BorderSide(color: Colors.blueAccent),
                    borderRadius: BorderRadius.circular(32.0)
                ),
                focusedBorder: OutlineInputBorder(
                  borderSide: BorderSide(color: Colors.blueAccent,width: 2.0),
                  borderRadius: BorderRadius.circular(32.0),
                ),
                hintText: 'Enter Your Name',
              ),
              validator: (value){
                if(value.isEmpty){
                  return 'Name is Required';
                }
              }
          )
        ],
      ),
    );
  }
  Widget _textFieldForPhone(){
    return Form(

      key: _formforphone,
      child: TextFormField(
          controller: _phone,
          keyboardType: TextInputType.phone,
          decoration: InputDecoration(
            contentPadding: EdgeInsets.symmetric(vertical: 10.0,horizontal: 20.0),
            border: OutlineInputBorder(
              borderSide: BorderSide(color:Colors.blueAccent,width: 1.0),
              borderRadius: BorderRadius.circular(32.0),
            ),
            enabledBorder: OutlineInputBorder(
                borderSide: BorderSide(color: Colors.blueAccent),
                borderRadius: BorderRadius.circular(32.0)
            ),
            focusedBorder: OutlineInputBorder(
              borderSide: BorderSide(color: Colors.blueAccent,width: 2.0),
              borderRadius: BorderRadius.circular(32.0),
            ),
            hintText: 'Enter Your Name',
          ),
          validator: _validateMobile,
      ),
    );
  }




  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        backgroundColor: Colors.white70,
        body: SingleChildScrollView(
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: 24.0),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Hero(tag: 'log', child: Container(
                  height: 200.0,
                  child: Image.asset('images/log.jpeg',height: 700,),
                )),
                SizedBox(
                  height: 48.00,
                ),
                _textFieldForName(),

                SizedBox(
                  height: 15.0,
                ),

                SizedBox(
                  height: 15.0,
                ),
                _textFieldForPhone(),

                Rounded_Button(title: 'Log In',color: Colors.blueAccent ,onPressed: (){
                   String hi= getData();

                  final isvalid= _form.currentState.validate();
                  if(!isvalid) {
                    return;}
                  _form.currentState.save();
                  final valid= _formforphone.currentState.validate();
                  if(!valid) {
                    return;}
                  _formforphone.currentState.save();
                  if(_phone==hi){
                    Navigator.pushNamed(context, MyHomePage.id);

                  }
                  else{
                    print('Not allow to go ahead');
                  }





                },),

              ],
            ),
          ),
        ),
      ),
    );
  }
}

回答1:


Your build() method should not start the loading of any data. Instead it should take its data from the state, and render that.

Typically I kick off any code that loads data, or authentication flows, in the constructor of your stateful component.

_LoginState() {
    List<String> store=[];
    String name;
    for(var snapshot in _firestore.collection('users').snapshots()){
      for(var phon in snapshot.documents){
        name=(phon.data['Phone']);
        store=[name];
      }
      setState((){
        store: store
      });
    }
}

Then you can use the store from the state in your build method like this:

@override
Widget build(BuildContext context) {
  ...

  Rounded_Button(title: 'Log In',color: Colors.blueAccent ,onPressed: (){
    var hi = this.store;
    ...
  })

  ...
}


来源:https://stackoverflow.com/questions/61487131/how-to-use-one-field-of-firebase-to-login

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!