firebase - 如何使用颤振从firebase实时检索数据
问题描述
我在下面有一个屏幕
当我单击配置文件按钮时,我想从我的 firebase 实时数据库中检索当前配置文件的所有数据并显示在下一页(下图)上。
这是我的编辑配置文件屏幕设计代码。
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,
),
],
),
),
),
)
],
),
),
),
);
}
}
我是 Flutter 和 Firebase 数据库的新手,任何人都可以帮助我。这是我的 Uni 项目。谢谢
解决方案
要检索数据,请尝试以下操作:
db = FirebaseDatabase.instance.reference().child("Users");
db.once().then((DataSnapshot snapshot){
Map<dynamic, dynamic> values = snapshot.value;
values.forEach((key,values) {
print(values["Email"]);
});
});
添加对节点的引用,Users
然后迭代并检索数据。要检索一个特定用户的数据,您需要使用查询,因为您没有使用 firebase auth :
db.orderByChild("Full Name").equalTo("Dasun Shanaka").once()
推荐阅读
- angular - 除了 spring boot 的功能之外,Jhipster 如何支持 thymeleaf?
- javascript - 从 1 开始每个 jquery
- python - 将函数的结果附加到列表中
- laravel - 我是 laravel 的新手,我想在主页上宣布
- mysql - SQL - 如果 col1 的值期望为 0 并且 col2 的值除了 0 之外,col3 的值相同,那么一行中的值
- sql - 如何在不更改存储过程本身的情况下将动态存储过程的结果(带输入参数)插入临时表
- javascript - 响应式汉堡菜单不适用于移动 Safari
- c# - 绑定属性即将为空
- sql - 使用计算列选择和分组的 SQL Server 问题
- dart - 如何根据文本大小扩展文本和容器?