![](/img/trans.png)
[英]How to read data from Firebase Realtime database using Flutter/Dart
[英]How to store data for each user in Flutter Firebase Realtime Database?
我正在使用 Flutter 构建移动应用程序。 对于待办事项列表功能,我将其连接到 firebase 实时数据库,以便在将值添加到应用程序时,这些值会在 firebase 数据库中自动更新。 它已成功完成,但我想要实现的是存储由用户分隔的数据。 就像用户在一个 firebase 实时数据库中拥有自己独立的数据库。
例如,如果 A 人添加了待办事项列表数据,则只有 A 可以查看它并在他登录时添加更多他的数据。而 B 人和 C 无法查看和访问 A 的数据,只能查看和访问他们自己添加的数据。 他们可以添加在登录时只能显示给他们自己的数据。我搜索并检查了许多文档,但找不到对我有帮助的文档。
谁能帮我实现这个功能? 我还想确定如果我更改数据库规则,是否需要将它的一些代码添加到应用程序中。
下面是目前firebase实时数据库规则。
{
"rules": {
".read": true,
".write": true
}
}
这是我的待办事项列表代码。
class ToDo extends StatefulWidget {
@override
_ToDoState createState() => _ToDoState();
}
class _ToDoState extends State<ToDo> {
final fb = FirebaseDatabase.instance;
@override
Widget build(BuildContext context) {
final ref = fb.ref().child('todos');
return Scaffold(
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.indigo[900],
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => addnote(),
),
);
},
child: Icon(
Icons.add,
),
),
appBar: AppBar(
title: Text(
'Todos',
style: TextStyle(
fontSize: 30,
),
),
backgroundColor: Colors.white,
),
body: FirebaseAnimatedList(
query: ref,
shrinkWrap: true,
itemBuilder: (context, snapshot, animation, index) {
return GestureDetector(
onTap: () {},
child: Container(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: ListTile(
shape: RoundedRectangleBorder(
side: BorderSide(
color: Colors.white,
),
borderRadius: BorderRadius.circular(10),
),
tileColor: Colors.indigo[100],
trailing: IconButton(
icon: Icon(
Icons.delete,
color: Colors.red[900],
),
onPressed: () {
ref.child(snapshot.key!).remove();
},
),
title: Text(
snapshot.value.toString(),
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
),
),
),
),
),
);
},
),
);
}
}
class addnote extends StatelessWidget {
TextEditingController title = TextEditingController();
final fb = FirebaseDatabase.instance;
@override
Widget build(BuildContext context) {
final ref = fb.ref().child('todos');
return Scaffold(
appBar: AppBar(
title: Text("Add Todos"),
backgroundColor: Colors.indigo[900],
),
body: Container(
child: Column(
children: [
SizedBox(
height: 10,
),
Container(
decoration: BoxDecoration(border: Border.all()),
child: TextField(
controller: title,
decoration: InputDecoration(
hintText: 'title',
),
),
),
SizedBox(
height: 10,
),
MaterialButton(
color: Colors.indigo[900],
onPressed: () {
ref
.push()
.set(
title.text,
)
.asStream();
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (_) => ToDo()));
},
child: Text(
"save",
style: TextStyle(
color: Colors.white,
fontSize: 20,
),
),
),
],
),
),
);
}
}
谢谢
您可以使用用户的 UID 创建一个子节点,如下所示:
todos/
├─ user1/
│ ├─ todoId1
├─ user2/
│ ├─ todoId2
这样,您只能使用以下规则限制用户对其条目的访问:
{
"rules": {
"todos": {
"$uid": {
".read": "$uid === auth.uid",
".write": "$uid === auth.uid"
}
}
}
}
您必须将代码中的DatabaseReference
更改为 UID 节点,而不仅仅是 todos,以便推送的任何新项目都将在该用户下:
// Use this ref to:
// 1. Fetch all TODOs
// 2. push() new TODO
final ref = fb.ref().child('todos').child(CURRENT_USER_ID);
查看文档以了解有关安全规则的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.