我正在使用 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,
),
),
),
],
),
),
);
}
}
謝謝
uj5u.com熱心網友回復:
您可以使用用戶的 UID 創建一個子節點,如下所示:
todos/
├─ user1/
│ ├─ todoId1
├─ user2/
│ ├─ todoId2
這樣,您只能使用以下規則限制用戶對其條目的訪問:
{
"rules": {
"todos": {
"$uid": {
".read": "$uid === auth.uid",
".write": "$uid === auth.uid"
}
}
}
}
您必須將DatabaseReference
代碼中的
// Use this ref to:
// 1. Fetch all TODOs
// 2. push() new TODO
final ref = fb.ref().child('todos').child(CURRENT_USER_ID);
查看檔案以了解有關安全規則的更多資訊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/506351.html
標籤:Google Cloud Collective 数据库 扑 火力基地 镖 firebase-实时数据库