导航到新屏幕时更改 flutter 应用程序中的用户首选项
Changing user preferences in a flutter app when navigating to new screen
我的 flutter 应用程序中有 2 个屏幕,它们的定义如下:
allFriends
:在卡片上有一个用户列表,单击它会显示用户的完整视图 (otherUserProfileView
)。
otherUserProfileView
:显示个人资料视图(信息从 Firebase 实时数据库加载)
当我导航到 otherUserProfileView
时,我仍然会看到我在重新加载后第一次查看的有关该用户的内容。
您知道我该如何解决这个问题,以便在单击新用户后每次导航到 otherUserProfileView
时都能看到新用户吗?感谢任何帮助。
代码如下:
allFriends
:
child: InkWell(
onTap: (){
Navigator.of(context).push(MaterialPageRoute(
builder: (context) =>
Screen2(
friendID: friend.userID,
friendName: friend.name,
profilePic: friend.picture,
),
));
},
child: Card()
)
otherUserProfileView
:
class OtherUserProfileView extends StatefulWidget {
final String friendID;
final String friendName;
final String profilePic;
const OtherUserProfileView(
{Key? key,
required this.friendID,
required this.friendName,
required this.profilePic})
: super(key: key);
@override
_OtherUserProfileViewState createState() => _OtherUserProfileViewState();
}
class _OtherUserProfileViewState extends State<OtherUserProfileView> {
List<String> images = [];
StreamSubscription? _imagesStream;
@override
void initState() {
super.initState();
addImages();
}
void addImages() {
images.add(widget.profilePic);
final db = FirebaseDatabase.instance
.ref()
.child('users')
.child(widget.friendID)
.child("pictures");
_imagesStream = db.onValue.listen((event) {
if (event.snapshot.exists) {
final data = new Map<dynamic, dynamic>.from(
event.snapshot.value as Map<dynamic, dynamic>);
data.forEach((key, value) {
db.child(key).onValue.listen((event) {
setState(() {
images.add(value);
});
});
});
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
extendBodyBehindAppBar: true,
body: _getContent(),
);
}
Widget _getContent() {
return new ListView(
scrollDirection: Axis.vertical,
children: <Widget>[
CardRow(friendID: widget.friendID),
images == null
? Container()
: Column(
children: [
Container(
height: 200,
child: ListView.builder(
scrollDirection: Axis.horizontal,
shrinkWrap: true,
itemBuilder: (context, index) =>
BuildPicture(images[index]),
itemCount: images.length,
),
),
],
),
],
);
}
@override
void deactivate() {
_imagesStream?.cancel();
super.deactivate();
}
}
class BuildPicture extends StatelessWidget {
final String url;
BuildPicture(this.url);
@override
Widget build(BuildContext context) {
return Container(
height: 200,
child: Image.network(url),
);
}
}
class CardRow extends StatefulWidget {
final String friendID;
const CardRow({Key? key, required this.friendID}) : super(key: key);
@override
State<CardRow> createState() => _CardRowState();
}
class _CardRowState extends State<CardRow> {
late StreamSubscription _userStream;
late StreamSubscription _friendsStream;
static String uid = "";
static DatabaseReference userDatabase =
FirebaseDatabase.instance.ref().child('users').child("$uid");
static List<String> theirFriends = [];
var _userName = "";
var _emailAddress = "";
var _country = "";
var _bio = "";
var _profileUrl;
var user;
int mutualFriends = theirFriends.length;
@override
void initState() {
super.initState();
uid = widget.friendID;
_activateListeners();
_retrieveFriends();
}
void _activateListeners() {
_userStream = userDatabase.onValue.listen((event) {
if (event.snapshot.exists) {
final data = new Map<dynamic, dynamic>.from(
event.snapshot.value as Map<dynamic, dynamic>);
final username = data['username'] as String;
final emailAdd = data['emailAdd'] as String;
final country = data['country'] as String;
final bio = data['bio'] as String;
final profilePicUrl = data['profilePicUrl'] as String;
setState(() {
_userName = username;
_emailAddress = emailAdd;
_country = country;
_bio = bio;
_profileUrl = profilePicUrl;
});
}
});
}
void _retrieveFriends() {
final friendsDb = FirebaseDatabase.instance
.ref()
.child('users')
.child(uid)
.child("friends");
_friendsStream = friendsDb.onValue.listen((event) {
if (event.snapshot.exists) {
final data = new Map<dynamic, dynamic>.from(
event.snapshot.value as Map<dynamic, dynamic>);
theirFriends.clear();
data.forEach((key, value) {
friendsDb.child(key).onValue.listen((event) {
final acc = new Map<dynamic, dynamic>.from(
event.snapshot.value as Map<dynamic, dynamic>);
final userID = acc['userID'] as String;
theirFriends.add(userID);
});
});
}
});
}
@override
Widget build(BuildContext context) {
return userCardContent();
}
Container userCardContent() {
return new Container(
child: ClipOval(
child: SizedBox(
width: 110,
height: 110,
child: (_profileUrl != null)
? Image.network(_profileUrl, fit: BoxFit.cover)
: Image.asset(
'assets/Blank-Avatar.png',
fit: BoxFit.cover,
)),
),
);
}
@override
void deactivate() {
_userStream.cancel();
_friendsStream.cancel();
super.deactivate();
}
}
即使使用的 id 是正确的,但其中一个子类没有更新,所以使用的 id 是错误的
为了找到这个问题的原因,我通过在需要使用它的地方打印 id 来追踪它。
我的 flutter 应用程序中有 2 个屏幕,它们的定义如下:
allFriends
:在卡片上有一个用户列表,单击它会显示用户的完整视图 (otherUserProfileView
)。otherUserProfileView
:显示个人资料视图(信息从 Firebase 实时数据库加载)
当我导航到 otherUserProfileView
时,我仍然会看到我在重新加载后第一次查看的有关该用户的内容。
您知道我该如何解决这个问题,以便在单击新用户后每次导航到 otherUserProfileView
时都能看到新用户吗?感谢任何帮助。
代码如下:
allFriends
:
child: InkWell(
onTap: (){
Navigator.of(context).push(MaterialPageRoute(
builder: (context) =>
Screen2(
friendID: friend.userID,
friendName: friend.name,
profilePic: friend.picture,
),
));
},
child: Card()
)
otherUserProfileView
:
class OtherUserProfileView extends StatefulWidget {
final String friendID;
final String friendName;
final String profilePic;
const OtherUserProfileView(
{Key? key,
required this.friendID,
required this.friendName,
required this.profilePic})
: super(key: key);
@override
_OtherUserProfileViewState createState() => _OtherUserProfileViewState();
}
class _OtherUserProfileViewState extends State<OtherUserProfileView> {
List<String> images = [];
StreamSubscription? _imagesStream;
@override
void initState() {
super.initState();
addImages();
}
void addImages() {
images.add(widget.profilePic);
final db = FirebaseDatabase.instance
.ref()
.child('users')
.child(widget.friendID)
.child("pictures");
_imagesStream = db.onValue.listen((event) {
if (event.snapshot.exists) {
final data = new Map<dynamic, dynamic>.from(
event.snapshot.value as Map<dynamic, dynamic>);
data.forEach((key, value) {
db.child(key).onValue.listen((event) {
setState(() {
images.add(value);
});
});
});
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
extendBodyBehindAppBar: true,
body: _getContent(),
);
}
Widget _getContent() {
return new ListView(
scrollDirection: Axis.vertical,
children: <Widget>[
CardRow(friendID: widget.friendID),
images == null
? Container()
: Column(
children: [
Container(
height: 200,
child: ListView.builder(
scrollDirection: Axis.horizontal,
shrinkWrap: true,
itemBuilder: (context, index) =>
BuildPicture(images[index]),
itemCount: images.length,
),
),
],
),
],
);
}
@override
void deactivate() {
_imagesStream?.cancel();
super.deactivate();
}
}
class BuildPicture extends StatelessWidget {
final String url;
BuildPicture(this.url);
@override
Widget build(BuildContext context) {
return Container(
height: 200,
child: Image.network(url),
);
}
}
class CardRow extends StatefulWidget {
final String friendID;
const CardRow({Key? key, required this.friendID}) : super(key: key);
@override
State<CardRow> createState() => _CardRowState();
}
class _CardRowState extends State<CardRow> {
late StreamSubscription _userStream;
late StreamSubscription _friendsStream;
static String uid = "";
static DatabaseReference userDatabase =
FirebaseDatabase.instance.ref().child('users').child("$uid");
static List<String> theirFriends = [];
var _userName = "";
var _emailAddress = "";
var _country = "";
var _bio = "";
var _profileUrl;
var user;
int mutualFriends = theirFriends.length;
@override
void initState() {
super.initState();
uid = widget.friendID;
_activateListeners();
_retrieveFriends();
}
void _activateListeners() {
_userStream = userDatabase.onValue.listen((event) {
if (event.snapshot.exists) {
final data = new Map<dynamic, dynamic>.from(
event.snapshot.value as Map<dynamic, dynamic>);
final username = data['username'] as String;
final emailAdd = data['emailAdd'] as String;
final country = data['country'] as String;
final bio = data['bio'] as String;
final profilePicUrl = data['profilePicUrl'] as String;
setState(() {
_userName = username;
_emailAddress = emailAdd;
_country = country;
_bio = bio;
_profileUrl = profilePicUrl;
});
}
});
}
void _retrieveFriends() {
final friendsDb = FirebaseDatabase.instance
.ref()
.child('users')
.child(uid)
.child("friends");
_friendsStream = friendsDb.onValue.listen((event) {
if (event.snapshot.exists) {
final data = new Map<dynamic, dynamic>.from(
event.snapshot.value as Map<dynamic, dynamic>);
theirFriends.clear();
data.forEach((key, value) {
friendsDb.child(key).onValue.listen((event) {
final acc = new Map<dynamic, dynamic>.from(
event.snapshot.value as Map<dynamic, dynamic>);
final userID = acc['userID'] as String;
theirFriends.add(userID);
});
});
}
});
}
@override
Widget build(BuildContext context) {
return userCardContent();
}
Container userCardContent() {
return new Container(
child: ClipOval(
child: SizedBox(
width: 110,
height: 110,
child: (_profileUrl != null)
? Image.network(_profileUrl, fit: BoxFit.cover)
: Image.asset(
'assets/Blank-Avatar.png',
fit: BoxFit.cover,
)),
),
);
}
@override
void deactivate() {
_userStream.cancel();
_friendsStream.cancel();
super.deactivate();
}
}
即使使用的 id 是正确的,但其中一个子类没有更新,所以使用的 id 是错误的
为了找到这个问题的原因,我通过在需要使用它的地方打印 id 来追踪它。