如何使用flatter从firebase实时检索数据
下面有一个屏幕 当我点击profile按钮时,我想从firebase实时数据库中检索当前配置文件的所有数据,并显示在下一页(下图) 这些是我想在上一页显示的数据。 这是我的编辑配置文件屏幕设计代码如何使用flatter从firebase实时检索数据,firebase,flutter,firebase-realtime-database,dart,Firebase,Flutter,Firebase Realtime Database,Dart,下面有一个屏幕 当我点击profile按钮时,我想从firebase实时数据库中检索当前配置文件的所有数据,并显示在下一页(下图) 这些是我想在上一页显示的数据。 这是我的编辑配置文件屏幕设计代码 Widget _NameTextField(String _name) { return Column( crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[
Widget _NameTextField(String _name) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Full Name',
style: kLabelStyle,
),
SizedBox(height: 10.0),
Container(
alignment: Alignment.centerLeft,
decoration: kBoxDecorationStyle,
height: 60.0,
child: TextFormField(
autovalidate: false,
style: TextStyle(
color: Colors.white,
fontFamily: 'OpenSans',
),
decoration: InputDecoration(
border: InputBorder.none,
contentPadding: EdgeInsets.only(top: 14.0),
prefixIcon: Icon(
Icons.perm_identity,
color: Colors.white,
),
hintText: 'Enter your Full Name',
hintStyle: kHintTextStyle,
),
initialValue: _name,
validator: (String value) {
if (value.isEmpty) {
return 'Name is Required.';
}
if (value.length < 3) {
return 'Name must be more than 2 charater';
}
return null;
},
onSaved: (String value) {
return _name = value;
},
),
),
],
);
}
Widget _NICTextField() {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'NIC No:',
style: kLabelStyle,
),
SizedBox(height: 10.0),
Container(
alignment: Alignment.centerLeft,
decoration: kBoxDecorationStyle,
height: 60.0,
child: TextFormField(
keyboardType: TextInputType.text,
autovalidate: false,
style: TextStyle(
color: Colors.white,
fontFamily: 'OpenSans',
),
decoration: InputDecoration(
border: InputBorder.none,
contentPadding: EdgeInsets.only(top: 14.0),
prefixIcon: Icon(
Icons.assignment_ind,
color: Colors.white,
),
hintText: 'Enter your NIC Number',
hintStyle: kHintTextStyle,
),
validator: (String value) {
if (value.isEmpty) {
return 'NIC is Required.';
}
return null;
},
onSaved: (String value) {
return _nic = value;
},
),
),
],
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
leading: IconButton(
icon:Icon(Icons.close),
onPressed:() => Navigator.of(context).pop(null),
),
centerTitle: true ,
title: Text('Edit Profile',
style: TextStyle(
color: Colors.white,
fontSize: 20.0,
fontWeight: FontWeight.bold,
),
),
actions: <Widget>[
GestureDetector(
onTap: ()async{
print('Profile Updated!!!');
},
child: Container(
padding: EdgeInsets.only(top: 17, right: 25),
child: Text(
'SAVE',
style: TextStyle(fontSize: 18),
),
),
),
],
),
body: AnnotatedRegion<SystemUiOverlayStyle>(
value: SystemUiOverlayStyle.light,
child: GestureDetector(
onTap: () => FocusScope.of(context).unfocus(),
child: Stack(
children: <Widget>[
Container(
height: double.infinity,
width: double.infinity,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Color(0xFF73AEF5),
Color(0xFF61A4F1),
Color(0xFF478DE0),
Color(0xFF398AE5),
],
stops: [0.1, 0.4, 0.7, 0.9],
),
),
),
Container(
height: double.infinity,
child: SingleChildScrollView(
physics: AlwaysScrollableScrollPhysics(),
padding: EdgeInsets.symmetric(
horizontal: 35.0,
vertical: 5.0,
),
child: Form(
key: formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SizedBox(height: 15.0),
imageAvatar,
_NameTextField(_name),
SizedBox(
height: 15.0,
),
_NICTextField(),
SizedBox(
height: 15.0,
),
],
),
),
),
)
],
),
),
),
);
}
}
Widget\u name文本字段(字符串\u name){
返回列(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
正文(
“全名”,
风格:克拉贝尔风格,
),
尺寸箱(高度:10.0),
容器(
对齐:alignment.centerLeft,
装饰:kBoxDecorationStyle,
身高:60.0,
子项:TextFormField(
自动验证:false,
样式:TextStyle(
颜色:颜色,白色,
fontFamily:“OpenSans”,
),
装饰:输入装饰(
边框:InputBorder.none,
contentPadding:仅限边集(顶部:14.0),
前缀:图标(
Icons.perm_标识,
颜色:颜色,白色,
),
hintText:'输入您的全名',
hintStyle:kHintTextStyle,
),
initialValue:\u名称,
验证器:(字符串值){
if(value.isEmpty){
返回“必须输入名称”;
}
如果(值.长度<3){
返回“名称必须超过2个字符”;
}
返回null;
},
onSaved:(字符串值){
返回_name=value;
},
),
),
],
);
}
小部件_NICTextField(){
返回列(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
正文(
“NIC编号:”,
风格:克拉贝尔风格,
),
尺寸箱(高度:10.0),
容器(
对齐:alignment.centerLeft,
装饰:kBoxDecorationStyle,
身高:60.0,
子项:TextFormField(
键盘类型:TextInputType.text,
自动验证:false,
样式:TextStyle(
颜色:颜色,白色,
fontFamily:“OpenSans”,
),
装饰:输入装饰(
边框:InputBorder.none,
contentPadding:仅限边集(顶部:14.0),
前缀:图标(
Icons.assignment\u ind,
颜色:颜色,白色,
),
hintText:'输入您的NIC号码',
hintStyle:kHintTextStyle,
),
验证器:(字符串值){
if(value.isEmpty){
返回“需要NIC”;
}
返回null;
},
onSaved:(字符串值){
返回_nic=值;
},
),
),
],
);
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
自动嵌入:false,
领先:IconButton(
图标:图标(Icons.close),
onPressed:()=>Navigator.of(context.pop)(null),
),
标题:对,
标题:文本(“编辑配置文件”,
样式:TextStyle(
颜色:颜色,白色,
字体大小:20.0,
fontWeight:fontWeight.bold,
),
),
行动:[
手势检测器(
onTap:()异步{
打印('Profile Updated!!!');
},
子:容器(
填充:仅限边缘设置(顶部:17,右侧:25),
子:文本(
“保存”,
样式:TextStyle(字体大小:18),
),
),
),
],
),
正文:德雷吉翁(
值:SystemUIOverlyStyle.light,
儿童:手势检测器(
onTap:()=>FocusScope.of(context.unfocus(),
子:堆栈(
儿童:[
容器(
高度:双无限,
宽度:double.infinity,
装饰:盒子装饰(
梯度:线性梯度(
开始:Alignment.topCenter,
结束:对齐。底部中心,
颜色:[
颜色(0xFF73AEF5),
颜色(0xFF61A4F1),
颜色(0xFF478DE0),
颜色(0xFF398AE5),
],
停止:[0.1,0.4,0.7,0.9],
),
),
),
容器(
高度:双无限,
子:SingleChildScrollView(
物理:AlwaysScrollableScrollPhysics(),
填充:EdgeInsets.symmetric(
水平线:35.0,
垂直线:5.0,
),
孩子:表格(
key:formKey,
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
尺寸箱(高度:15.0),
图像化身,
_NameTextField(_name),
大小盒子(
身高:15.0,
),
_NICTextField(),
大小盒子(
身高:15.0,
),
],
),
),
),
)
],
),
),
),
);
}
}
我是新来的扑动和firebase数据库有人能帮我吗。这是我的大学项目。
谢谢要检索数据,请尝试以下操作:
db = FirebaseDatabase.instance.reference().child("Users");
db.once().then((DataSnapshot snapshot){
Map<dynamic, dynamic> values = snapshot.value;
values.forEach((key,values) {
print(values["Email"]);
});
});
要检索数据,请尝试以下操作:
db = FirebaseDatabase.instance.reference().child("Users");
db.once().then((DataSnapshot snapshot){
Map<dynamic, dynamic> values = snapshot.value;
values.forEach((key,values) {
print(values["Email"]);
});
});