这是我将数据写入Firebase的地方
class userContent {
final String uid;
userContent({this.uid});
bool isLoggedIn() {
if (FirebaseAuth.instance.currentUser() != null) {
return true;
} else {
return false;
}
}
Future<Void> addData(propertyData) async {
if (isLoggedIn()) {
Firestore.instance
.collection('userContent')
.document('posts')
.collection('postCollection')
.add(propertyData)
.catchError((e) {
print(e);
});
} else {
print('You need to be logged in');
}
}
getData() async {
return await Firestore.instance
.collection('userContent')
.document('posts')
.collection('postCollection')
.snapshots();
}
updateData(selectedDoc, newValues) {
Firestore.instance
.collection('userContent')
.document('posts')
.collection('postCollection')
.document(selectedDoc)
.updateData(newValues)
.catchError((e) {
print(e);
});
}
deleteData(docId) {
Firestore.instance
.collection('userContent')
.document('posts')
.collection('postCollection')
.document(docId)
.delete()
.catchError((e) {
print(e);
});
}
UserContent _userDataFromSnapshot(QuerySnapshot snapshot) {
return UserContent(
uid: uid,
);
}
Stream<UserContent> get userDataStream {
return Firestore.instance
.collection('userContent')
.document('posts')
.collection(uid)
.snapshots()
.map(_userDataFromSnapshot);
}
}
这是我用来显示对话框的地方,用户可以在其中向Firebase存储添加新属性,
Future<bool> addDialog(BuildContext context) async {
final user = Provider.of<User>(context);
return showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Add Data', style: TextStyle(fontSize: 15.0)),
content: SingleChildScrollView(
child: Form(
key: _formKey,
child: Column(
children: <Widget>[
TextFormField(
validator: (val) =>
val.isEmpty ? 'Enter the property\'s title' : null,
decoration:
InputDecoration(hintText: 'Enter property title'),
onChanged: (value) {
this.propertyTitle = value;
},
),
SizedBox(height: 5.0),
TextFormField(
validator: (val) =>
val.isEmpty ? 'Enter the property\'s address' : null,
decoration: InputDecoration(
hintText: 'Enter the property\'s Address'),
onChanged: (value) {
this.propetyAddress = value;
},
),
SizedBox(height: 5.0),
TextFormField(
validator: (val) =>
val.isEmpty ? 'Enter the property\'s postcode' : null,
decoration: InputDecoration(
hintText: 'Enter the property\'s potcode'),
onChanged: (value) {
this.propertyPostcode = value;
},
),
SizedBox(height: 5.0),
TextFormField(
validator: (val) =>
val.isEmpty ? 'Enter the property\'s price' : null,
decoration: InputDecoration(hintText: 'Enter a price'),
onChanged: (value) {
this.propertyPrice = value;
},
),
SizedBox(height: 5.0),
TextFormField(
validator: (val) => val.isEmpty
? 'Enter sales type \'(For Sale or To rent)\''
: null,
decoration:
InputDecoration(hintText: 'Enter selling type'),
onChanged: (value) {
this.sellingType = value;
},
),
SizedBox(height: 5.0),
TextFormField(
validator: (val) =>
val.isEmpty ? 'Enter a description' : null,
decoration:
InputDecoration(hintText: 'Enter Detail Description'),
onChanged: (value) {
this.moreDetails = value;
},
),
DropdownButtonFormField(
value: numberOfbaths ?? '1',
items: bathrooms.map((bathrooms) {
return DropdownMenuItem(
value: bathrooms,
child: Text('$bathrooms bathrooms'),
);
}).toList(),
onChanged: (String value) {
this.numberOfbaths = value;
},
),
DropdownButtonFormField(
value: numberOfbedrooms ?? '1',
items: bedrooms.map((bedrooms) {
return DropdownMenuItem(
value: bedrooms,
child: Text('$bedrooms bedrooms'),
);
}).toList(),
onChanged: (String value) {
this.numberOfbedrooms = value;
},
),
DropdownButtonFormField(
value: numberOflivingrooms ?? '1',
items: livingrooms.map((livingrooms) {
return DropdownMenuItem(
value: livingrooms,
child: Text('$livingrooms livingrooms'),
);
}).toList(),
onChanged: (String value) {
this.numberOflivingrooms = value;
},
),
TextFormField(
validator: (val) =>
val.isEmpty ? 'Enter energy costs per month' : null,
decoration:
InputDecoration(hintText: 'Energy Costs per month'),
onChanged: (value) {
this.energyPermonth = value;
},
),
TextFormField(
validator: (val) =>
val.isEmpty ? 'Enter tax costs per month' : null,
decoration:
InputDecoration(hintText: 'Tax Costs per month'),
onChanged: (value) {
this.taxPermonth = value;
},
),
TextFormField(
validator: (val) =>
val.isEmpty ? 'Enter water costs per month' : null,
decoration:
InputDecoration(hintText: 'Water Costs per month'),
onChanged: (value) {
this.waterPermonth = value;
},
),
],
),
),
),
actions: <Widget>[
FlatButton(
child: Text('Add'),
textColor: Colors.blue,
onPressed: () {
if (_formKey.currentState.validate()) {
Navigator.of(context).pop();
userContent(uid: user.uid).addData({
'uid': user.uid,
'Property Title': this.propertyTitle,
'Seller Name': this.sellerName,
'Property Address': this.propetyAddress,
'Property Postcode': this.propertyPostcode,
'Property Price': this.propertyPrice,
'Selling Type': this.sellingType,
'more Details': this.moreDetails,
'Number of Bathrooms': this.numberOfbaths,
'Number of Bedrooms': this.numberOfbedrooms,
'Number of Livingrooms': this.numberOflivingrooms,
'Energy per Month': this.energyPermonth,
'Tax per Month': this.taxPermonth,
'Water per Month': this.waterPermonth,
}).catchError((e) {
print(e);
print(user.uid);
});
}
},
)
],
);
});
}
我已经弄清楚如何包含uid,但是我不知道如何使用它来删除文档。 我对堆栈流有很多不同的建议,但是我很难解决。 拜托,有人可以帮助我和其他会遇到同样问题的人吗?
答案 0 :(得分:0)
这应该有效。别忘了使其同步
await Firestore.instance
.collection('userContent') .
document('posts') .
collection('postCollection') .
document(uid) .delete()