fix: flutter remove setState in initState (#8807)

Signed-off-by: dignow <linlong1265@gmail.com>
This commit is contained in:
dignow 2024-07-24 14:00:49 +08:00 committed by GitHub
parent 57d1b1ecc4
commit 79a1f888d6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 69 additions and 31 deletions

View File

@ -78,6 +78,13 @@ class _PeerTabPageState extends State<PeerTabPage>
@override
void initState() {
WidgetsBinding.instance.addPostFrameCallback((_) {
_loadLocalOptions();
});
super.initState();
}
Future<void> _loadLocalOptions() async {
final uiType = bind.getLocalFlutterOption(k: kOptionPeerCardUiType);
if (uiType != '') {
peerCardUiType.value = int.parse(uiType) == 0
@ -88,7 +95,6 @@ class _PeerTabPageState extends State<PeerTabPage>
}
hideAbTagsPanel.value =
bind.mainGetLocalOption(key: kOptionHideAbTagsPanel) == 'Y';
super.initState();
}
Future<void> handleTabSelection(int tabIndex) async {
@ -875,18 +881,22 @@ class _PeerSortDropdownState extends State<PeerSortDropdown> {
@override
void initState() {
if (!PeerSortType.values.contains(peerSort.value)) {
Future.delayed(Duration.zero, () {
// do not change obx directly in initState, so do in future.
peerSort.value = PeerSortType.remoteId;
bind.setLocalFlutterOption(
k: kOptionPeerSorting,
v: peerSort.value,
);
// do not change obx directly in initState, so do in future.
WidgetsBinding.instance.addPostFrameCallback((_) {
_loadLocalOptions();
});
}
super.initState();
}
Future<void> _loadLocalOptions() async {
peerSort.value = PeerSortType.remoteId;
bind.setLocalFlutterOption(
k: kOptionPeerSorting,
v: peerSort.value,
);
}
@override
Widget build(BuildContext context) {
final style = TextStyle(

View File

@ -98,7 +98,10 @@ class _FileManagerPageState extends State<FileManagerPage>
}
debugPrint("File manager page init success with id ${widget.id}");
_ffi.dialogManager.setOverlayState(_overlayKeyState);
widget.tabController.onSelected?.call(widget.id);
// Call onSelected in post frame callback, since we cannot guarantee that the callback will not call setState.
WidgetsBinding.instance.addPostFrameCallback((_) {
widget.tabController.onSelected?.call(widget.id);
});
}
@override

View File

@ -65,7 +65,10 @@ class _PortForwardPageState extends State<PortForwardPage>
isRdp: widget.isRDP);
Get.put<FFI>(_ffi, tag: 'pf_${widget.id}');
debugPrint("Port forward page init success with id ${widget.id}");
widget.tabController.onSelected?.call(widget.id);
// Call onSelected in post frame callback, since we cannot guarantee that the callback will not call setState.
WidgetsBinding.instance.addPostFrameCallback((_) {
widget.tabController.onSelected?.call(widget.id);
});
}
@override

View File

@ -135,11 +135,13 @@ class _RemotePageState extends State<RemotePage>
if (!isWeb) bind.pluginSyncUi(syncTo: kAppTypeDesktopRemote);
_ffi.qualityMonitorModel.checkShowQualityMonitor(sessionId);
_ffi.dialogManager.loadMobileActionsOverlayVisible();
// Session option should be set after models.dart/FFI.start
_showRemoteCursor.value = bind.sessionGetToggleOptionSync(
sessionId: sessionId, arg: 'show-remote-cursor');
_zoomCursor.value = bind.sessionGetToggleOptionSync(
sessionId: sessionId, arg: kOptionZoomCursor);
WidgetsBinding.instance.addPostFrameCallback((_) {
// Session option should be set after models.dart/FFI.start
_showRemoteCursor.value = bind.sessionGetToggleOptionSync(
sessionId: sessionId, arg: 'show-remote-cursor');
_zoomCursor.value = bind.sessionGetToggleOptionSync(
sessionId: sessionId, arg: kOptionZoomCursor);
});
DesktopMultiWindow.addListener(this);
// if (!_isCustomCursorInited) {
// customCursorController.registerNeedUpdateCursorCallback(
@ -154,7 +156,10 @@ class _RemotePageState extends State<RemotePage>
// }
_blockableOverlayState.applyFfi(_ffi);
widget.tabController?.onSelected?.call(widget.id);
// Call onSelected in post frame callback, since we cannot guarantee that the callback will not call setState.
WidgetsBinding.instance.addPostFrameCallback((_) {
widget.tabController?.onSelected?.call(widget.id);
});
}
@override

View File

@ -129,7 +129,7 @@ class ConnectionManagerState extends State<ConnectionManager>
if (client != null) {
gFFI.chatModel.changeCurrentKey(MessageKey(client.peerId, client.id));
if (client.unreadChatMessageCount.value > 0) {
Future.delayed(Duration.zero, () {
WidgetsBinding.instance.addPostFrameCallback((_) {
client.unreadChatMessageCount.value = 0;
gFFI.chatModel.showChatPage(MessageKey(client.peerId, client.id));
});
@ -399,7 +399,10 @@ class _CmHeaderState extends State<_CmHeader>
_time.value = _time.value + 1;
}
});
gFFI.serverModel.tabController.onSelected?.call(client.id.toString());
// Call onSelected in post frame callback, since we cannot guarantee that the callback will not call setState.
WidgetsBinding.instance.addPostFrameCallback((_) {
gFFI.serverModel.tabController.onSelected?.call(client.id.toString());
});
}
@override
@ -732,7 +735,8 @@ class _CmControlPanel extends StatelessWidget {
child: buildButton(context,
color: MyTheme.accent,
onClick: null, onTapDown: (details) async {
final devicesInfo = await AudioInput.getDevicesInfo(true, true);
final devicesInfo =
await AudioInput.getDevicesInfo(true, true);
List<String> devices = devicesInfo['devices'] as List<String>;
if (devices.isEmpty) {
msgBox(
@ -764,7 +768,8 @@ class _CmControlPanel extends StatelessWidget {
value: d,
groupValue: currentDevice,
onChanged: (v) {
if (v != null) AudioInput.setDevice(v, true, true);
if (v != null)
AudioInput.setDevice(v, true, true);
},
child: Container(
child: Text(

View File

@ -48,6 +48,12 @@ class MyPopupMenuItemState<T, W extends PopupMenuChildrenItem<T>>
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
_initEnabled();
});
}
Future<void> _initEnabled() async {
if (widget.enabled != null) {
enabled.value = widget.enabled!.value;
}

View File

@ -372,7 +372,7 @@ class _RemoteToolbarState extends State<RemoteToolbar> {
initState() {
super.initState();
Future.delayed(Duration.zero, () async {
WidgetsBinding.instance.addPostFrameCallback((_) async {
_fractionX.value = double.tryParse(await bind.sessionGetOption(
sessionId: widget.ffi.sessionId,
arg: 'remote-menubar-drag-x') ??
@ -1278,7 +1278,9 @@ class _ResolutionsMenuState extends State<_ResolutionsMenu> {
@override
void initState() {
super.initState();
_getLocalResolutionWayland();
WidgetsBinding.instance.addPostFrameCallback((_) {
_getLocalResolutionWayland();
});
}
Rect? scaledRect() {

View File

@ -1271,12 +1271,14 @@ class ActionIcon extends StatefulWidget {
}
class _ActionIconState extends State<ActionIcon> {
var hover = false.obs;
final hover = false.obs;
@override
void initState() {
super.initState();
hover.value = false;
WidgetsBinding.instance.addPostFrameCallback((_) {
hover.value = false;
});
}
@override

View File

@ -55,14 +55,14 @@ class _ConnectionPageState extends State<ConnectionPage> {
super.initState();
if (!isWeb) _uniLinksSubscription = listenUniLinks();
if (_idController.text.isEmpty) {
() async {
WidgetsBinding.instance.addPostFrameCallback((_) async {
final lastRemoteId = await bind.mainGetLastRemoteId();
if (lastRemoteId != _idController.id) {
setState(() {
_idController.id = lastRemoteId;
});
}
}();
});
}
if (isAndroid) {
if (!bind.isCustomClient()) {

View File

@ -83,9 +83,11 @@ class _RemotePageState extends State<RemotePage> {
initSharedStates(widget.id);
gFFI.chatModel
.changeCurrentKey(MessageKey(widget.id, ChatModel.clientModeID));
gFFI.chatModel.voiceCallStatus.value = VoiceCallStatus.notStarted;
WidgetsBinding.instance.addPostFrameCallback((_) {
gFFI.chatModel.voiceCallStatus.value = VoiceCallStatus.notStarted;
gFFI.dialogManager.loadMobileActionsOverlayVisible();
});
_blockableOverlayState.applyFfi(gFFI);
gFFI.dialogManager.loadMobileActionsOverlayVisible();
}
@override

View File

@ -118,7 +118,7 @@ class _SettingsState extends State<SettingsPage> with WidgetsBindingObserver {
_hideNetwork =
bind.mainGetBuildinOption(key: kOptionHideNetworkSetting) == 'Y';
() async {
WidgetsBinding.instance.addPostFrameCallback((_) async {
var update = false;
if (_hasIgnoreBattery) {
@ -177,7 +177,7 @@ class _SettingsState extends State<SettingsPage> with WidgetsBindingObserver {
if (update) {
setState(() {});
}
}();
});
}
@override

View File

@ -2,7 +2,7 @@ use hbb_common::{bail, platform::windows::is_windows_version_or_greater, ResultT
use std::sync::atomic;
// This string is defined here.
// https://github.com/rustdesk/RustDeskIddDriver/blob/b370aad3f50028b039aad211df60c8051c4a64d6/RustDeskIddDriver/RustDeskIddDriver.inf#LL73C1-L73C40
// https://github.com/rustdesk-org/RustDeskIddDriver/blob/b370aad3f50028b039aad211df60c8051c4a64d6/RustDeskIddDriver/RustDeskIddDriver.inf#LL73C1-L73C40
pub const RUSTDESK_IDD_DEVICE_STRING: &'static str = "RustDeskIddDriver Device\0";
pub const AMYUNI_IDD_DEVICE_STRING: &'static str = "USB Mobile Monitor Virtual Display\0";