From a983756e38503791a4be30132862aeb1a54f4e6a Mon Sep 17 00:00:00 2001 From: Nahrawene-ben-hmida Date: Mon, 13 Jan 2025 14:07:55 +0000 Subject: [PATCH] fix LateInitializationError --- lib/src/toast.dart | 240 +++++++++++++++++++++++---------------------- 1 file changed, 121 insertions(+), 119 deletions(-) diff --git a/lib/src/toast.dart b/lib/src/toast.dart index bd9835d..69d9050 100644 --- a/lib/src/toast.dart +++ b/lib/src/toast.dart @@ -1171,131 +1171,133 @@ class BotToast { ///如果为[close]则关闭该toast,并且拦截事件停止冒泡 ///如果为null or [none]则不拦截事件 /// - static CancelFunc showEnhancedWidget({ - required ToastBuilder toastBuilder, - UniqueKey? key, - String? groupKey, - /*bool*/ Object crossPage = nil, - /*bool*/ Object allowClick = nil, - /*bool*/ Object clickClose = nil, - /*bool*/ Object ignoreContentClick = nil, - /*bool*/ Object onlyOne = nil, - /*bool*/ Object enableKeyboardSafeArea = nil, - BackButtonBehavior? backButtonBehavior = nilBackButtonBehavior, - FutureFunc? closeFunc = nilFutureFunc, - VoidCallback? onClose = nilVoidCallback, - Color backgroundColor = nilColor, - WrapWidget? warpWidget = nilWrapWidget, - Duration? duration = nilDuration, - }) { - // ignore: unnecessary_null_comparison - assert(enableKeyboardSafeArea != null); - - var o = defaultOption.enhanced; - - crossPage = returnFirstIfNotNil(crossPage, o.crossPage); - allowClick = returnFirstIfNotNil(allowClick, o.allowClick); - clickClose = returnFirstIfNotNil(clickClose, o.clickClose); - ignoreContentClick = returnFirstIfNotNil(ignoreContentClick, o.ignoreContentClick); - onlyOne = returnFirstIfNotNil(onlyOne, o.onlyOne); - enableKeyboardSafeArea = returnFirstIfNotNil(enableKeyboardSafeArea, o.enableKeyboardSafeArea); - backButtonBehavior = returnFirstIfNotNil(backButtonBehavior, o.backButtonBehavior); - closeFunc = returnFirstIfNotNil(closeFunc, o.closeFunc); - onClose = returnFirstIfNotNil(onClose, o.onClose); - backgroundColor = returnFirstIfNotNil(backgroundColor, o.backgroundColor); - warpWidget = returnFirstIfNotNil(warpWidget, o.warpWidget); - duration = returnFirstIfNotNil(duration, o.duration); - - assert(isNilOr([enableKeyboardSafeArea, onlyOne, clickClose, allowClick, ignoreContentClick, crossPage]), 'Must be of bool type'); +static CancelFunc showEnhancedWidget({ + required ToastBuilder toastBuilder, + UniqueKey? key, + String? groupKey, + /*bool*/ Object crossPage = nil, + /*bool*/ Object allowClick = nil, + /*bool*/ Object clickClose = nil, + /*bool*/ Object ignoreContentClick = nil, + /*bool*/ Object onlyOne = nil, + /*bool*/ Object enableKeyboardSafeArea = nil, + BackButtonBehavior? backButtonBehavior = nilBackButtonBehavior, + FutureFunc? closeFunc = nilFutureFunc, + VoidCallback? onClose = nilVoidCallback, + Color backgroundColor = nilColor, + WrapWidget? warpWidget = nilWrapWidget, + Duration? duration = nilDuration, +}) { + // ignore: unnecessary_null_comparison + assert(enableKeyboardSafeArea != null); + + var o = defaultOption.enhanced; + + crossPage = returnFirstIfNotNil(crossPage, o.crossPage); + allowClick = returnFirstIfNotNil(allowClick, o.allowClick); + clickClose = returnFirstIfNotNil(clickClose, o.clickClose); + ignoreContentClick = returnFirstIfNotNil(ignoreContentClick, o.ignoreContentClick); + onlyOne = returnFirstIfNotNil(onlyOne, o.onlyOne); + enableKeyboardSafeArea = returnFirstIfNotNil(enableKeyboardSafeArea, o.enableKeyboardSafeArea); + backButtonBehavior = returnFirstIfNotNil(backButtonBehavior, o.backButtonBehavior); + closeFunc = returnFirstIfNotNil(closeFunc, o.closeFunc); + onClose = returnFirstIfNotNil(onClose, o.onClose); + backgroundColor = returnFirstIfNotNil(backgroundColor, o.backgroundColor); + warpWidget = returnFirstIfNotNil(warpWidget, o.warpWidget); + duration = returnFirstIfNotNil(duration, o.duration); + + assert(isNilOr([enableKeyboardSafeArea, onlyOne, clickClose, allowClick, ignoreContentClick, crossPage]), 'Must be of bool type'); + + // Declare cancelFunc as non-late and initialize it + CancelFunc cancelFunc = () {}; + + final CancelFunc dismissFunc = () async { + await closeFunc?.call(); + cancelFunc(); + }; - //由于cancelFunc一开始是为空的,所以在赋值之前需要在闭包里使用 - late final CancelFunc cancelFunc; - final CancelFunc dismissFunc = () async { - await closeFunc?.call(); - cancelFunc(); - }; + //onlyOne 功能 + final List cache = (cacheCancelFunc[groupKey ?? defaultKey] ??= []); + if (onlyOne == true) { + final clone = cache.toList(); + cache.clear(); + clone.forEach((cancel) { + cancel(); + }); + } + cache.add(dismissFunc); + + //定时功能 + Timer? timer; + if (duration != null) { + timer = Timer(duration, () { + dismissFunc(); + timer = null; + }); + } - //onlyOne 功能 - final List cache = (cacheCancelFunc[groupKey ?? defaultKey] ??= []); - if (onlyOne == true) { - final clone = cache.toList(); - cache.clear(); - clone.forEach((cancel) { - cancel(); - }); - } - cache.add(dismissFunc); - - //定时功能 - Timer? timer; - if (duration != null) { - timer = Timer(duration, () { - dismissFunc(); - timer = null; - }); - } + //跨页自动关闭 + BotToastNavigatorObserverProxy? observerProxy; + if (!(crossPage == true)) { + observerProxy = BotToastNavigatorObserverProxy.all(dismissFunc); + BotToastNavigatorObserver.register(observerProxy); + } - //跨页自动关闭 - BotToastNavigatorObserverProxy? observerProxy; - if (!(crossPage == true)) { - observerProxy = BotToastNavigatorObserverProxy.all(dismissFunc); - BotToastNavigatorObserver.register(observerProxy); - } + //拦截点击返回事件 + VoidCallback? unRegisterFunc; + if (backButtonBehavior == BackButtonBehavior.ignore) { + unRegisterFunc = BotToastWidgetsBindingObserver.singleton.registerPopListener(() { + return true; + }); + } else if (backButtonBehavior == BackButtonBehavior.close) { + unRegisterFunc = BotToastWidgetsBindingObserver.singleton.registerPopListener(() { + dismissFunc(); + unRegisterFunc?.call(); + unRegisterFunc = null; + return true; + }); + } - //拦截点击返回事件 - VoidCallback? unRegisterFunc; - if (backButtonBehavior == BackButtonBehavior.ignore) { - unRegisterFunc = BotToastWidgetsBindingObserver.singleton.registerPopListener(() { - return true; - }); - } else if (backButtonBehavior == BackButtonBehavior.close) { - unRegisterFunc = BotToastWidgetsBindingObserver.singleton.registerPopListener(() { - dismissFunc(); - unRegisterFunc?.call(); - unRegisterFunc = null; - return true; - }); + cancelFunc = showWidget( + groupKey: groupKey, + key: key, + toastBuilder: (_) { + return KeyboardSafeArea( + enable: enableKeyboardSafeArea == true, + child: ProxyDispose(disposeCallback: () { + cache.remove(dismissFunc); + if (observerProxy != null) { + BotToastNavigatorObserver.unregister(observerProxy); + } + timer?.cancel(); + onClose?.call(); + unRegisterFunc?.call(); + }, child: Builder(builder: (BuildContext context) { + final TextStyle textStyle = Theme.of(context).textTheme.bodyMedium!; + Widget child = DefaultTextStyle( + style: textStyle, + child: Stack(children: [ + Listener( + onPointerDown: clickClose == true ? (_) => dismissFunc() : null, + behavior: allowClick == true ? HitTestBehavior.translucent : HitTestBehavior.opaque, + child: const SizedBox.expand(), + ), + IgnorePointer( + child: Container(color: backgroundColor), + ), + IgnorePointer( + ignoring: ignoreContentClick == true, + child: toastBuilder(dismissFunc), + ) + ])); + return warpWidget != null ? warpWidget(dismissFunc, child) : child; + })), + ); } + ); - cancelFunc = showWidget( - groupKey: groupKey, - key: key, - toastBuilder: (_) { - return KeyboardSafeArea( - enable: enableKeyboardSafeArea == true, - child: ProxyDispose(disposeCallback: () { - cache.remove(dismissFunc); - if (observerProxy != null) { - BotToastNavigatorObserver.unregister(observerProxy); - } - timer?.cancel(); - onClose?.call(); - unRegisterFunc?.call(); - }, child: Builder(builder: (BuildContext context) { - final TextStyle textStyle = Theme.of(context).textTheme.bodyMedium!; - Widget child = DefaultTextStyle( - style: textStyle, - child: Stack(children: [ - Listener( - onPointerDown: clickClose == true ? (_) => dismissFunc() : null, - behavior: allowClick == true ? HitTestBehavior.translucent : HitTestBehavior.opaque, - child: const SizedBox.expand(), - ), - IgnorePointer( - child: Container(color: backgroundColor), - ), - IgnorePointer( - ignoring: ignoreContentClick == true, - child: toastBuilder(dismissFunc), - ) - ])); - return warpWidget != null ? warpWidget(dismissFunc, child) : child; - })), - ); - }); - - return dismissFunc; - } + return dismissFunc; +} /// Displays a Widget on the screen that can persist across multiple pages. /// [toastBuilder] is the builder function to generate the Widget to be displayed.