firebase - 为什么 streamBuilder 不打印电话字段?
问题描述
我在 onpressed {} 中编写此代码并且不返回任何电话号码电话字段中存在许多电话号码:实际上我想确认用户用于登录的电话号码必须存在于 Firestore 中。如果 Firestore 中不存在它,则会显示错误。
我的 StreamBuiler 是
StreamBuilder<QuerySnapshot>(
stream: _firestore.collection('users').snapshots(),
builder: (context, snapshot){
if(!snapshot.hasData){
return Center(
child: CircularProgressIndicator(
backgroundColor: Colors.lightBlueAccent,
),
);
}
final ph=snapshot.data.documents;
List<String> store=[];
for(var phonenum in ph){
final catA=phonenum.data['Phone'];
store=[catA];
print(store);
};
}
);
并且我的 login.dart 正在关注任何告诉我在以下代码中哪里出错的人。谢谢。
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;
// _LoginState() {
// 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];
// }
// setState((){
// stor: 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: (){
StreamBuilder<QuerySnapshot>(
stream: _firestore.collection('users').snapshots(),
builder: (context, snapshot){
if(!snapshot.hasData){
return Center(
child: CircularProgressIndicator(
backgroundColor: Colors.lightBlueAccent,
),
);
}
final ph=snapshot.data.documents;
List<String> store=[];
for(var phonenum in ph){
final catA=phonenum.data['Phone'];
store=[catA];
print(store);
};
print(store);
}
);
},
),
// var hi = this.store;
// 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');
// }
],
),
),
),
),
);
}
}
解决方案
推荐阅读
- python - 无法在新文件中复制时间变量
- ansible - ansible 与基于字典的默认变量
- admob - 如何提高 google admob 匹配率?
- dialogflow-es - 如何获得谷歌助手的回复
- javascript - javascript:修改导入的“变量”会导致“分配给常量变量”,即使它不是常量
- spring-boot - Intellij IDEA 和 JPA 控制台“hibernate.dialect”在查询 Sql Server 表期间未设置错误
- php - PHP/Laravel 日期时间自动更改
- jquery - 使用 UIKit 3 向下滚动时删除粘性导航栏
- java - 服务器上的 Exchange 信任库,无需停止服务器
- python - python 3.x range 函数计算 sum Einrückung