getx
getx copied to clipboard
why wasn't onclose method executed in getxcontroller ?
i want to comply “Scroll controller” in getx, but when i Get.toNamed to B from A,To A from B again. i find that "scontroller.dispose()" wasn't executed in onClose() method;
Terminal showed error message : "Another exception was thrown: Assertion failed: file:///E:/Program%20Files/flutter/flutter_windows_2.8.1-stable/flutter/packages/flutter/lib/src/widgets/scroll_controller.dart:108:12 Another exception was thrown: The provided ScrollController is currently attached to more than one ScrollPosition.
Another exception was thrown: The provided ScrollController is currently attached to more than one ScrollPosition.
Another exception was thrown: The provided ScrollController is currently attached to more than one ScrollPosition. Another exception was thrown: Assertion failed: file:///E:/Program%20Files/flutter/flutter_windows_2.8.1-stable/flutter/packages/flutter/lib/src/widgets/scroll_controller.dart:108:12 Another exception was thrown: The provided ScrollController is currently attached to more than one ScrollPosition.
Another exception was thrown: The provided ScrollController is currently attached to more than one ScrollPosition. Another exception was thrown: The provided ScrollController is currently attached to more than one ScrollPosition. Another exception was thrown: Assertion failed: file:///E:/Program%20Files/flutter/flutter_windows_2.8.1-stable/flutter/packages/flutter/lib/src/widgets/scroll_controller.dart:108:12 Another exception was thrown: The provided ScrollController is currently attached to more than one ScrollPosition.
Another exception was thrown: The provided ScrollController is currently attached to more than one ScrollPosition.
Another exception was thrown: The provided ScrollController is currently attached to more than one ScrollPosition. Another exception was thrown: Assertion failed: file:///E:/Program%20Files/flutter/flutter_windows_2.8.1-stable/flutter/packages/flutter/lib/src/widgets/scroll_controller.dart:108:12 Another exception was thrown: The provided ScrollController is currently attached to more than one ScrollPosition. Another exception was thrown: Assertion failed: file:///E:/Program%20Files/flutter/flutter_windows_2.8.1-stable/flutter/packages/flutter/lib/src/widgets/scroll_controller.dart:108:12 Another exception was thrown: The provided ScrollController is currently attached to more than one ScrollPosition. Another exception was thrown: The provided ScrollController is currently attached to more than one ScrollPosition."
code : page A : `
final GetArticlesController a = Get.put(GetArticlesController());
@override Widget build(context) { return WillPopScope( onWillPop: () async { Get.delete<GetArticlesController>(); return true; }, child: Scaffold( appBar: MainAppbar(context), // 通过判断枚举值 来 进行选择对应的 页面变化 以确定数据加载的结果 body: GetBuilder<GetArticlesController>( builder: ((controller) {
return SingleChildScrollView(
// 给列表添加屏幕监听控件
controller: controller.scontroller,
physics: const ScrollPhysics(),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextButton(
onPressed: () {
Get.toNamed(
"/article/${controller.articleList[index]["id"]}");
},
child: Text(
controller.articleList[index]
["title"],
textAlign: TextAlign.center,
style: const TextStyle(
color: Colors.black,
fontSize: 32,
fontWeight: FontWeight.w500),
),
),
),
);`
controller A: ` class GetArticlesController extends GetxController { // 创建 滚动监听 组件 final ScrollController scontroller = ScrollController(); bool showTopBtn = false;
// 控制器的初始化方法,组件在内存分配后会被马上调用,可以在这个方法对 controller 做一些初始化工作
@override void onInit() { print("初始化");
print("start: $showTopBtn");
ScreenCt();
super.onInit();
}
@override void onReady() { print("onReady");
super.onReady();
}
@override void onClose() { // 为了避免内存泄漏,需要调用dispose scontroller.dispose(); print("onclse"); update(); super.onClose(); }
// 监听 鼠标滚动组件位置函数
// ignore: non_constant_identifier_names
void ScreenCt() {
// 打印 初始化监听地址
scontroller.addListener(() {
// 监听位置需要 在 小于 500 并且 showTopBtn 为true 时生效
if (scontroller.offset < 500 && showTopBtn) {
//tionButton 设为false 生效 (但是此处使用的是: ! Showtopbtn 取反操作)
print("打印监听位置f" + scontroller.offset.toString());
showTopBtn = false;
// 此时 按钮应该消失
print("位置小于500: $showTopBtn");
// update();
} else if (scontroller.offset >= 500 && showTopBtn == false) {
// 此时按钮应该 出现
print("打印监听位置t" + scontroller.offset.toString());
showTopBtn = true;
print("位置大于500: $showTopBtn");
// update();
}
update();
});
}
}
doctor:
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.0.2, on Microsoft Windows [版本 10.0.19044.1889], locale zh-CN)
[√] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
[√] Chrome - develop for the web
[X] Visual Studio - develop for Windows
X Visual Studio not installed; this is necessary for Windows development.
Download at https://visualstudio.microsoft.com/downloads/.
Please install the "Desktop development with C++" workload, including all of its default components
[!] Android Studio (not installed)
[√] VS Code
[√] Connected device (3 available)
[√] HTTP Host Availability
! Doctor found issues in 2 categories.`
get: ^4.6.5
who can tell me that how I should do? please!