gpt4 book ai didi

firebase - 具有多个监听器的全局流

转载 作者:行者123 更新时间:2023-12-03 02:46:40 26 4
gpt4 key购买 nike

我有一个连接到 Firebase 文档的流,我一直在监听文档的任何更改。

我想在文档更改时更新我的​​应用程序的某些部分,因此我使流全局可用,并且我在不同的屏幕上使用 StreamBuilders,因此最新的数据将在屏幕上可用。

全局流和多个StreamBuilders会不会有问题?在多个屏幕上创建 StreamBuilders 时,应用程序如何工作?

这是我的全局流:

Stream userDocGlobalStream =
Firestore.instance.collection("user").document(CurrentUserDetails.id).snapshots();



这是我的屏幕小部件之一的构建方法(我根据流数据更改按钮的颜色):
  @override
Widget build(BuildContext context) {
final ThemeData theme = Theme.of(context);
return StreamBuilder(
stream: userDocGlobalStream,
builder: (context, snapShot) {
return Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30),
side: BorderSide(color: theme.primaryColor)),
elevation: 30,
child: Container(
margin: EdgeInsets.all(10),
child: Column(
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment
.stretch, //need to use it to strech the items horizontally
children: <Widget>[
Container(
//to think, it is used for the background color of the picture. can undo it later
decoration: BoxDecoration(
color: theme.primaryColor.withAlpha(10),
borderRadius: BorderRadius.circular(10),
),
padding: EdgeInsets.all(10),
child: GestureDetector(
onTap: () {}, //go to user profile when pressed.
child: CircleAvatar(
radius: 70,
backgroundImage: NetworkImage(userImageUrl),
),
),
),
],
),
Container(
margin: EdgeInsets.symmetric(vertical: 10),
child: FittedBox(
child: Text(
"Username : $username ",
),
),
),
Container(
margin: EdgeInsets.symmetric(vertical: 10),
child: FittedBox(
child: Text(
"interests : ${interests.toString().replaceAll("[", "").replaceAll("]", "")} ",
),
),
),
Container(
margin: EdgeInsets.symmetric(vertical: 10),
child: FittedBox(
child: Text("Distance from you : $distanceFromUser KM"),
),
), //to do address and km from you should go here
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
IconButton(
color: theme.primaryColor,
icon: Icon(Icons.chat),
onPressed: () {}, //send a message to the user
),
IconButton(
color: Colors.cyan,
icon: CurrentUserDetails.friendRequestsSent.contains(
userId) //to do=> THE ICON MUST CHANGE WITH EVERY CHANGE OF THE DATA
? Icon(
Icons.person,
color: Colors.black,
)
: CurrentUserDetails.friends.contains(userId)
? Icon(
Icons.person,
color: Colors.green,
)
: Icon(Icons.person_add),
onPressed: () {
try {
//to do
CurrentUserDetails.friendRequestsSent
.contains(userId)
? DoNothingAction()
//Cancel the sent request:
: CurrentUserDetails.friendRequestsReceived
.contains(userId)
? DoNothingAction()
//accept friend request:
: CurrentUserDetails.friends
.contains(userId)
? DoNothingAction()
//delete the friend:
: DatabaseManagement().sendFriendRequest(
CurrentUserDetails.id,
userId); //userId is the id of the user we are showing the widget for
} catch (e) {
showDialog(
context: context,
builder: (ctx) => DialogueWidget(
titleText: "Error occured",
contentText:
e.toString(), //to do+> change the er
),
);
}
} //send friend request when this button is pressed
),
IconButton(
color: Colors.red[300],
icon: Icon(Icons.location_on),
onPressed:
() {}, //show a map with a users location details on it.
)
],
)
],
),
),
);
},
);
}
}

最佳答案

StreamBuilder自动开始和结束监听提供的 stream: ,因此在应用程序周围的多个位置使用一个广播流时不会有问题。即使嵌套监听一个流也不是问题。

如果您想深入挖掘,这里有一些有用的链接:

  • https://dart.dev/tutorials/language/streams
  • https://api.flutter.dev/flutter/widgets/StreamBuilder-class.html
  • 关于firebase - 具有多个监听器的全局流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60169826/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com