我只想在一个文档被更改或更新时得到通知。获取更新的每个示例始终使用集合。我试图仅使用一个文档就实现了它,但是它从未得到任何更新。这是我现在无法使用的内容:
src
我已经调试了100次,但从未进入“ builder:”部分。顺便说一下,这对于文档参考不是问题。我在这里做什么错了?
答案 0 :(得分:7)
这是一个例子
export default function RadioInput() {
const { register, setValue } = useForm<FormData>();
const [inputValue1, setInputValue1] = useState('');
const [inputValue2, setInputValue2] = useState('');
const [isSelected1, setIsSelected1] = useState(false);
const [isSelected2, setIsSelected2] = useState (false);
const onChange1 = () => {
console.log(inputValue1)
setInputValue1(inputValue1)
setIsSelected1(!isSelected1)
};
const onChange2 = () => {
console.log(inputValue2)
setInputValue2(inputValue2)
setIsSelected2(!isSelected2)
};
const styles = {
radioPink: {
border: "10px solid green"
}
}
return (
<div>
<label className="radio">Company
<input type="radio" checked={isSelected1} value={inputValue1} onChange=.
{onChange1} name="is_company"/>
<span className="checkround"/>
</label>
<label className="radio">Company
<input type="radio" checked={isSelected2} value={inputValue2} onChange=
{onChange2} name="is_company"/>
<span className="checkround"/>
</label>
<button className="btn cust-btn " type="button" id="btn-registration"
>Register
</button>
</div>
);
}
但是您做错了什么?
Firestore.instance
.collection('Users')
.document(widget.uid)
.snapshots()
.listen((DocumentSnapshot documentSnapshot) {
Map<String, dynamic> firestoreInfo = documentSnapshot.data;
setState(() {
money = firestoreInfo['earnings'];
});
})
.onError((e) => print(e));
替换为
(BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
user = snapshot.data.data as User;
});
答案 1 :(得分:2)
String collPath = 'books';
String docPath= 'auNEAjG276cQ1C9IUltJ';
DocumentReferance documentReferance = firestoreInstance.collection(collPath).document(docPath);
documentReference.snapshots().listen((snapshot) {
print(snapshot.data);
}
答案 2 :(得分:0)
以下是使用StreamBuilder的解决方案:
StreamBuilder(
stream: Firestore.instance
.collection("sightings")
.doc(sighting.sightingID)
.snapshots(),
builder: (context, snapshot) {
sighting = Sighting.fromMap(snapshot.data.data()); // Gives you the data map
return Scaffold(
appBar: AppBar(
title: Text('Document Details'),
),
body: Column(
children: [
Text(sighting.type)
],
),
);
});
键是snapshot.data.data()行,该行返回文档中的数据图。
答案 3 :(得分:-1)
这是我的经验,工作正常。 (具有 BLoC 模式的 StreamBUilder )。
Step1 =>通过查询和限制进行过滤
var userQuery = Firestore.instance
.collection('tbl_users')
.where('id', isEqualTo: id)
.limit(1);
Step2 =>倾听
userQuery.snapshots().listen((data) {
data.documentChanges.forEach((change) {
print('documentChanges ${change.document.data}');
});
});
BLoC
class HomeBloc {
final userSc = StreamController<UserEntity>();
Future doGetProfileFireStore() async {
await SharedPreferencesHelper.getUserId().then((id) async {
print('$this SharedPreferencesHelper.getUserId() ${id}');
var userQuery = Firestore.instance
.collection('tbl_users')
.where('id', isEqualTo: id)
.limit(1);
await userQuery.getDocuments().then((data) {
print('$this userQuery.getDocuments()');
if (data.documents.length > 0) {
print('$this data found');
userQuery.snapshots().listen((data) {
data.documentChanges.forEach((change) {
print('documentChanges ${change.document.data}');
userSc.sink.add(new UserEntity.fromSnapshot(data.documents[0]));
});
});
} else {
print('$this data not found');
}
});
});
}
void dispose() {
userSc.close();
}
}
查看
new StreamBuilder(
stream: bloc.userSc.stream,
builder: (BuildContext context, AsyncSnapshot<UserEntity> user) {
return new Center(
child: new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
user.hasData
? new Container(
width: 80,
height: 80,
decoration: new BoxDecoration(
borderRadius: BorderRadius.circular(100.0),
image: new DecorationImage(
image: NetworkImage(user.data.photo),
fit: BoxFit.cover,
),
),
)
: new Container(
width: 50,
height: 50,
child: new CircularProgressIndicator(
strokeWidth: 2,
valueColor:
AlwaysStoppedAnimation<Color>(ColorsConst.base),
),
),
new Container(
margin: EdgeInsets.fromLTRB(10, 0, 0, 0),
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new Text(
user.hasData
? '${user.data.username.toUpperCase()}'
: 'loading',
style: TextStyleConst.b16(
color: Colors.black
.withOpacity(user.hasData ? 1.0 : 0.2),
letterSpacing: 2),
),
new Container(
margin: EdgeInsets.all(5),
),
new Text(
user.hasData ? '${user.data.bio}' : 'loading',
style: TextStyleConst.n14(
color: Colors.black
.withOpacity(user.hasData ? 1.0 : 0.2)),
),
new Container(
margin: EdgeInsets.fromLTRB(0, 10, 0, 0),
padding: EdgeInsets.all(10),
decoration: new BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(100.0),
),
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new Row(
children: <Widget>[
new Icon(
Icons.trending_up,
color: Colors.white,
size: 20,
),
new Text(
'145K',
style:
TextStyleConst.b14(color: Colors.white),
),
],
),
new Container(
margin: EdgeInsets.fromLTRB(10, 0, 10, 0),
),
new Row(
children: <Widget>[
new Icon(
Icons.trending_down,
color: Colors.white,
size: 20,
),
new Text(
'17',
style:
TextStyleConst.b14(color: Colors.white),
),
],
),
],
),
),
],
),
),
],
),
);
},
),