将基于userid的数据保存到firebase



我正在尝试制作一个财务跟踪器应用程序。在使用应用程序之前,我会使用Firebase身份验证为用户进行登录(有效(。但我想根据用户id记录用户的财务状况。我该怎么做?任何人都可以帮助我,因为我是新来的。对于下面的代码,事务存储在firebase中,但我不基于用户id。我只遵循Youtube上的教程,但我找不到基于用户id的教程。请帮助,谢谢!

class RecordExpense extends StatefulWidget {
@override
_RecordExpenseState createState() => _RecordExpenseState();
}

class _RecordExpenseState extends State<RecordExpense> {
DatabaseReference _ref;
final date = TextEditingController();
final category = TextEditingController();
final amount = TextEditingController();
final description = TextEditingController();
final FirebaseAuth auth = FirebaseAuth.instance;
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

@override
String selectExpense;

final expenseSelected = TextEditingController();

List <String> expensecategories = [
"Food",
"Social Life",
"Transportation",
"Beauty",
"Household",
"Education",
"Health",
"Gift",
"Other"
];
DateTime _selectedDate;

void initState(){
_ref = FirebaseDatabase.instance.reference().child('Transaction');
}
Widget build(BuildContext context) {
return new Form(
child: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(20.0),

child: Column(
key: _formKey,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Container(
child : TextField(
cursorColor: Colors.grey,
controller: date,
onTap: () {
_selectDate(context);
},
decoration: InputDecoration(
labelText: "Date",
labelStyle: TextStyle(fontSize: 18.0, color: Colors.black),
hintText: "Select Date",
enabledBorder: UnderlineInputBorder(

borderSide: BorderSide(color: secondary),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: secondary),
),
),
),
),
Container(
//padding: EdgeInsets.all(20.0),
child: TextField(
cursorColor: Colors.grey,
controller: amount,
decoration: InputDecoration(
labelText: "Amount",
labelStyle: TextStyle(fontSize: 18.0, color: Colors.black),
hintText: "Amount",
enabledBorder: UnderlineInputBorder(

borderSide: BorderSide(color: secondary),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: secondary),
),
),
keyboardType: TextInputType.number,
),
),
Container(
padding: EdgeInsets.only(top: 20.0),
child: Container(
decoration: BoxDecoration(
border : Border.all(color: secondary),
borderRadius: BorderRadius.circular(15.0),
),
child: DropDownField(
controller: expenseSelected,
hintText: "Select Category",
labelText: "Category",
enabled: true,
itemsVisibleInDropdown: 4,
items: expensecategories,
onValueChanged: (dynamic value)
{
selectExpense = value;
},
value: selectExpense,
required: false,
),
),
),


Container(
//padding: EdgeInsets.all(20,0),
child: TextField(
cursorColor: Colors.grey,
controller: description,
maxLines: 2,
decoration: InputDecoration(
labelText: "Descriptions",
labelStyle: TextStyle(fontSize: 18.0, color: Colors.black),
hintText: "Expense Description",
enabledBorder: UnderlineInputBorder(

borderSide: BorderSide(color: secondary),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: secondary),
),
),
),
),
Container(
padding: EdgeInsets.only(top: 25.0, left: 20.0, right: 20.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget> [
Expanded (
child: ElevatedButton(

onPressed: () {
saveRecord();
},
child: Text("Save".toUpperCase(), style: TextStyle (
fontSize: 14,
)),
style: ButtonStyle(
padding: MaterialStateProperty.all<EdgeInsets>(EdgeInsets.all(15)),
foregroundColor: MaterialStateProperty.all<Color>(Colors.white),
backgroundColor: MaterialStateProperty.all<Color>(Colors.pink),
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
side: BorderSide(color: secondary)
),
),
),

),
),
SizedBox(width: 20, height: 10),
Expanded(
child: ElevatedButton(
onPressed: () {
clearButton();
},
child: Text("Clear".toUpperCase(), style: TextStyle (
fontSize: 14
)),
style: ButtonStyle(
padding: MaterialStateProperty.all<EdgeInsets>(EdgeInsets.all(15)),
foregroundColor: MaterialStateProperty.all<Color>(Colors.white),
backgroundColor: MaterialStateProperty.all<Color>(Colors.pink),
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
side: BorderSide(color: secondary)
),
),
),
),
)
],
)
),
],
),
),
),
);
}
void saveRecord() async {
User user = await auth.currentUser;
//DocumentReference documentReference = Firestore.instance.collection("Transaction").document(user.uid);
String dates = date.text;
String values = amount.text;
String descriptions = description.text;

Map<String,String> transaction = {
'dates':dates,
'amount': 'RM'+ values,
'category': selectExpense,
'descriptions': descriptions,
};
_ref.push().set(transaction).then((value) {
Navigator.pop(context, TransactionDaily());
});
}
void clearButton(){
date.clear();
amount.clear();
category.clear();
description.clear();
}
void dispose(){
super.dispose();
date.dispose();
amount.dispose();
category.dispose();
description.dispose();
}
_selectDate(BuildContext context) async {
DateTime newSelectedDate = await showDatePicker(
context: context,
initialDate: _selectedDate != null ? _selectedDate : DateTime.now(),
firstDate: DateTime(2000),
lastDate: DateTime(2040),
builder: (BuildContext context, Widget child) {
return Theme(
data: ThemeData.dark().copyWith(
colorScheme: ColorScheme.dark(
primary: secondary,
onPrimary: Colors.black,
surface: primary,
onSurface: Colors.white,
),
dialogBackgroundColor: Colors.black,
),
child: child,
);
});

if (newSelectedDate != null) {
_selectedDate = newSelectedDate;
date
..text = DateFormat.yMMMd().format(_selectedDate)
..selection = TextSelection.fromPosition(TextPosition(
offset: date.text.length,
affinity: TextAffinity.upstream));
}
}
}
class AlwaysDisabledFocusNode extends FocusNode {
@override
bool get hasFocus => false;
}
DatabaseReference userId = _ref.push();
print('Pushed Key' + userId.key);
id = userId.key;
userId.set(data);

将其添加到void saveRecord((函数中。

在这里,您还将获得控制台上的按键数据。