如何使用颤振从firebase实时检索数据
Posted
技术标签:
【中文标题】如何使用颤振从firebase实时检索数据【英文标题】:How to retrieve data from firebase realtime with flutter 【发布时间】:2020-09-10 11:29:17 【问题描述】:我在下面有一个屏幕
当我点击配置文件按钮时,我想从我的 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 项目。 谢谢
【问题讨论】:
【参考方案1】:要检索数据,请尝试以下操作:
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()
【讨论】:
以上是关于如何使用颤振从firebase实时检索数据的主要内容,如果未能解决你的问题,请参考以下文章