Skip to content

[Bug report] _checkTagChange 方法中的错误可能导致组件 dispose 时断言失败 #143

@caocong1

Description

@caocong1

Version

1.26.2

Platforms

dart

Device Model

iPhone 17

flutter info

[✓] Flutter (Channel stable, 3.38.5, on macOS 26.1 25B78 darwin-arm64, locale zh-Hans-CN) [536ms]
    • Flutter version 3.38.5 on channel stable at /Users/caocong/fvm/versions/stable
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision f6ff1529fd (3 weeks ago), 2025-12-11 11:50:07 -0500
    • Engine revision 1527ae0ec5
    • Dart version 3.10.4
    • DevTools version 2.51.1
    • Feature flags: enable-web, enable-linux-desktop, enable-macos-desktop, enable-windows-desktop, enable-android, enable-ios, cli-animations,
      enable-native-assets, omit-legacy-version-file, enable-lldb-debugging

[✓] Android toolchain - develop for Android devices (Android SDK version 36.1.0-rc1) [4.7s]
    • Android SDK at /Users/caocong/Library/Android/sdk
    • Emulator version 36.2.12.0 (build_id 14214601) (CL:N/A)
    • Platform android-36, build-tools 36.1.0-rc1
    • ANDROID_HOME = /Users/caocong/Library/Android/sdk
    • ANDROID_SDK_ROOT = /Users/caocong/Library/Android/sdk
    • Java binary at: /Users/caocong/Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
      This is the JDK bundled with the latest Android Studio installation on this machine.
      To manually set the JDK path, use: `flutter config --jdk-dir="path/to/jdk"`.
    • Java version OpenJDK Runtime Environment (build 21.0.7+-13880790-b1038.58)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 26.2) [2.1s]
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 17C52
    • CocoaPods version 1.16.2

[✓] Chrome - develop for the web [7ms]
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Connected device (6 available) [6.5s]
    • Caocong的iPhone (mobile)              • 00008020-00065C313CF8002E            • ios            • iOS 18.5 22F76
    • iPhone xs max (wireless) (mobile) • 00008020-0005305634D8003A            • ios            • iOS 15.3.1 19D52
    • iPhone 17 Pro Max (mobile)        • 9B81FBE2-1572-416B-8429-FEE93753E0A3 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-26-0 (simulator)
    • iPhone Air (mobile)               • FBBEAAEC-BAB4-4143-8574-0ACA4DE86346 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-26-2 (simulator)
    • macOS (desktop)                   • macos                                • darwin-arm64   • macOS 26.1 25B78 darwin-arm64
    • Chrome (web)                      • chrome                               • web-javascript • Google Chrome 143.0.7499.170

[✓] Network resources [4.2s]
    • All expected network resources are available.

• No issues found!

How to reproduce?

问题描述
在 observer_widget.dart 第 553 行存在一个拼写错误,导致在切换 Tab 时组件被 dispose 后触发断言失败。
错误信息

flutter: [ErrorLogger] 'package:scrollview_observer/src/common/observer_widget.dart': Failed assertion: line 559 pos 12: '_scopeContext.mounted': is not true.
flutter: #2 ObserverWidgetState._checkTagChange (package:scrollview_observer/src/common/observer_widget.dart:559:12)
flutter:

问题原因
在 lib/src/common/observer_widget.dart 第 553 行:

void _checkTagChange(T oldWidget) async {
final oldTag = oldWidget.tag ?? '';
final tag = widget.tag ?? '';
if (tag == oldWidget) return; // ❌ 错误:比较 String 和 Widget,永远不会相等
// ...
}
问题:tag == oldWidget 比较的是 String 和 Widget 对象,永远不会为 true。 应该是:tag == oldTag(比较两个 String)
影响
由于这个条件永远不成立,方法会继续执行:
await WidgetsBinding.instance.endOfFrame; 等待当前帧结束
如果在等待期间组件被 dispose(例如切换 Tab),context 变为 unmounted
第 559 行 assert(_scopeContext.mounted) 断言失败
建议修复

  • if (tag == oldWidget) return;
  • if (tag == oldTag) return;

Logs

Example code (optional)

Contact

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions