From 2c0169e25b2ca32aafb4ab4199f428ff5786fcd0 Mon Sep 17 00:00:00 2001 From: Jolie <412895109@qq.com> Date: Thu, 27 Nov 2025 23:02:54 +0800 Subject: [PATCH] =?UTF-8?q?fix(live):=20=E4=BF=AE=E5=A4=8D=E7=9B=B4?= =?UTF-8?q?=E6=92=AD=E9=97=B4=E7=A4=BC=E7=89=A9=E5=BC=B9=E7=AA=97=E5=A4=B4?= =?UTF-8?q?=E9=83=A8=E6=98=BE=E7=A4=BA=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 调整 RoomController 获取位置,避免重复查找 - 当 RoomController 为空时,返回默认头部组件 - 优化 rtcChannelDetail 响应式变量访问方式 - 修复男性观众信息赋值错误问题 - 修复女性观众信息赋值错误问题 - 添加 userData 空值判断,防止空指针异常 --- lib/controller/discover/room_controller.dart | 46 ++++++++++---------- lib/widget/live/live_gift_popup.dart | 29 +++++++++--- 2 files changed, 45 insertions(+), 30 deletions(-) diff --git a/lib/controller/discover/room_controller.dart b/lib/controller/discover/room_controller.dart index c9fcfd6..ac2c495 100644 --- a/lib/controller/discover/room_controller.dart +++ b/lib/controller/discover/room_controller.dart @@ -466,19 +466,22 @@ class RoomController extends GetxController with WidgetsBindingObserver { if (currentDetail == null) { return; } + final userData = response.data.data; + if (userData == null) { + return; + } + if (message['role'] == 'male_audience') { - final userData = response.data.data; - // if (userData != null) { final maleInfo = RtcSeatUserInfo( - miId: rtcChannelDetail.value!.anchorInfo!.miId, - userId: rtcChannelDetail.value!.anchorInfo!.userId, - nickName: rtcChannelDetail.value!.anchorInfo!.nickName, - profilePhoto: rtcChannelDetail.value!.anchorInfo!.profilePhoto, - genderCode: rtcChannelDetail.value!.anchorInfo!.genderCode, - seatNumber: rtcChannelDetail.value!.anchorInfo!.seatNumber, - isFriend: rtcChannelDetail.value!.anchorInfo!.isFriend, - isMicrophoneOn: rtcChannelDetail.value!.anchorInfo!.isMicrophoneOn, - isVideoOn: rtcChannelDetail.value!.anchorInfo!.isVideoOn, + miId: userData.miId, + userId: userData.userId, + nickName: userData.nickName, + profilePhoto: userData.profilePhoto, + genderCode: userData.genderCode, + seatNumber: userData.seatNumber, + isFriend: userData.isFriend, + isMicrophoneOn: userData.isMicrophoneOn, + isVideoOn: userData.isVideoOn, uid: message['uid'] is int ? message['uid'] as int : int.tryParse(message['uid']?.toString() ?? ''), @@ -490,20 +493,17 @@ class RoomController extends GetxController with WidgetsBindingObserver { femaleInfo: currentDetail.femaleInfo, ); rtcChannelDetail.value = newDetail; - // } } else if (message['role'] == 'female_audience') { - final userData = response.data.data; - // if (userData != null) { final femaleInfo = RtcSeatUserInfo( - miId: rtcChannelDetail.value!.anchorInfo!.miId, - userId: rtcChannelDetail.value!.anchorInfo!.userId, - nickName: rtcChannelDetail.value!.anchorInfo!.nickName, - profilePhoto: rtcChannelDetail.value!.anchorInfo!.profilePhoto, - genderCode: rtcChannelDetail.value!.anchorInfo!.genderCode, - seatNumber: rtcChannelDetail.value!.anchorInfo!.seatNumber, - isFriend: rtcChannelDetail.value!.anchorInfo!.isFriend, - isMicrophoneOn: rtcChannelDetail.value!.anchorInfo!.isMicrophoneOn, - isVideoOn: rtcChannelDetail.value!.anchorInfo!.isVideoOn, + miId: userData.miId, + userId: userData.userId, + nickName: userData.nickName, + profilePhoto: userData.profilePhoto, + genderCode: userData.genderCode, + seatNumber: userData.seatNumber, + isFriend: userData.isFriend, + isMicrophoneOn: userData.isMicrophoneOn, + isVideoOn: userData.isVideoOn, uid: message['uid'] is int ? message['uid'] as int : int.tryParse(message['uid']?.toString() ?? ''), diff --git a/lib/widget/live/live_gift_popup.dart b/lib/widget/live/live_gift_popup.dart index 2e4daab..d53a361 100644 --- a/lib/widget/live/live_gift_popup.dart +++ b/lib/widget/live/live_gift_popup.dart @@ -135,14 +135,29 @@ class _LiveGiftPopupState extends State { } Widget _buildHeader() { - return Obx(() { - // 获取 RoomController - final roomController = Get.isRegistered() - ? Get.find() - : null; + // 获取 RoomController(在 Obx 外部获取,避免重复查找) + final roomController = Get.isRegistered() + ? Get.find() + : null; - // 获取 rtcChannelDetail - final rtcChannelDetail = roomController?.rtcChannelDetail.value; + if (roomController == null) { + return Container( + height: 53.w, + padding: EdgeInsets.symmetric(horizontal: 10.w), + child: Row( + children: [ + Text( + "送给: ", + style: TextStyle(fontSize: 13.w, color: Colors.white), + ), + ], + ), + ); + } + + return Obx(() { + // 直接访问响应式变量,让 Obx 能够正确监听变化 + final rtcChannelDetail = roomController.rtcChannelDetail.value; // 获取当前用户ID final currentUserId = GlobalData().userId ?? GlobalData().userData?.id;