fix(home): 修复 feed 更新触发的 build 期 setState

This commit is contained in:
Zhou Weidong committed 2026-10-21 19:45:38 +08:00
1 parent 862b7e92dd
commit d227ac69fb
6 files changed
+174 -10

No files matched your search

+26
View File
@@ -149,3 +149,29 @@ Running Gradle task 'assembleDebug'... 28.7s
解读:静态分析零告警,561 项全量测试通过,debug APK 构建成功;
`pubspec.lock` 已还原,无依赖变更,未安装或操作真机。
## 2026-10-21 K98.2 修复 feed 更新触发 build 期通知
- 已从最新 `origin/develop` 创建 `fix/k98.2-feed-build-notify`。
- PlayerPage 的 feed 更新改为帧后调度,同一帧多次变化继续由 PlayerLogic 合并。
- PlayerLogic 在非 idle 调度阶段延后 feed 通知与 PageController 跳页,并给延迟通知去重。
- HomeLogic.onDramaChanged 在 build/layout/paint 阶段改为帧后通知。
- 已补 build 阶段 updateFeed、最终 PageController 位置及 HomeLogic 延后通知回归测试。
门禁原始输出:
```text
$ dart analyze lib test
Analyzing lib, test...
No issues found!
$ flutter test --no-pub
00:55 +563: All tests passed!
$ flutter build apk --debug
Running Gradle task 'assembleDebug'... 26.4s
✓ Built build/app/outputs/flutter-apk/app-debug.apk
```
解读:静态分析零告警,563 项全量测试通过,debug APK 构建成功;
`pubspec.lock` 已还原,无依赖变更,未安装或操作真机。
+13 -1
View File
@@ -1,6 +1,7 @@
// Author: fruit.com.cn
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import '../../core/data/models/drama.dart';
import '../discover/discover_types.dart';
@@ -22,6 +23,7 @@ class HomeLogic extends ChangeNotifier {
bool drawerOpen = false;
bool detailOpen = false;
bool videoImmersive = false;
bool _dramaNotifyScheduled = false;
bool _disposed = false;
void notify() {
@@ -120,7 +122,17 @@ class HomeLogic extends ChangeNotifier {
void onDramaChanged(Drama drama) {
currentDrama = drama;
notify();
if (SchedulerBinding.instance.schedulerPhase == SchedulerPhase.idle) {
notify();
return;
}
if (_dramaNotifyScheduled || _disposed) return;
_dramaNotifyScheduled = true;
WidgetsBinding.instance.addPostFrameCallback((_) {
_dramaNotifyScheduled = false;
if (!_disposed) notify();
});
SchedulerBinding.instance.scheduleFrame();
}
void onImmersiveChanged(bool value) {
+26 -4
View File
@@ -3,6 +3,7 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:provider/provider.dart';
import '../../core/data/models/drama.dart';
@@ -108,6 +109,8 @@ class PlayerLogic extends ChangeNotifier {
bool _latestResetToFirst = false;
bool _feedUpdateRunning = false;
bool _hasPendingFeed = false;
bool _deferredFeedNotifyScheduled = false;
int _feedJumpSerial = 0;
Future<void> _feedUpdateFuture = Future<void>.value();
/// [start] 是否已经跑过:没跑过时 session/pages 还是 late 未初始化,
@@ -259,7 +262,7 @@ class PlayerLogic extends ChangeNotifier {
if (dramas.isNotEmpty) pageDrama = dramas.first;
if (nextQueue.length == 0) {
session.setRouteVisible(false);
notify();
_notifyAfterFrameIfNeeded();
return;
}
final keepIndex =
@@ -275,7 +278,7 @@ class PlayerLogic extends ChangeNotifier {
preloadIndex: nextVisibleIndex(keepIndex),
);
_jumpToFeedPage(keepIndex);
notify();
_notifyAfterFrameIfNeeded();
return;
}
await _replaceSession(
@@ -303,7 +306,7 @@ class PlayerLogic extends ChangeNotifier {
final nextSession = _createSession(factory, coordinator);
session = nextSession;
nextSession.setRouteVisible(active && visibleIndices.isNotEmpty);
notify();
_notifyAfterFrameIfNeeded();
await previous.close();
if (revision != feedRevision || !identical(session, nextSession)) return;
_jumpToFeedPage(targetIndex);
@@ -312,13 +315,32 @@ class PlayerLogic extends ChangeNotifier {
void _jumpToFeedPage(int index) {
if (!_started) return;
if (pages.hasClients) {
final serial = ++_feedJumpSerial;
if (SchedulerBinding.instance.schedulerPhase == SchedulerPhase.idle &&
pages.hasClients) {
pages.jumpToPage(index);
return;
}
WidgetsBinding.instance.addPostFrameCallback((_) {
if (_disposed || serial != _feedJumpSerial || !_started) return;
if (pages.hasClients) pages.jumpToPage(index);
});
SchedulerBinding.instance.scheduleFrame();
}
void _notifyAfterFrameIfNeeded() {
if (_disposed) return;
if (SchedulerBinding.instance.schedulerPhase == SchedulerPhase.idle) {
notify();
return;
}
if (_deferredFeedNotifyScheduled) return;
_deferredFeedNotifyScheduled = true;
WidgetsBinding.instance.addPostFrameCallback((_) {
_deferredFeedNotifyScheduled = false;
if (!_disposed) notify();
});
SchedulerBinding.instance.scheduleFrame();
}
/// 服务端续看位置:进播放页时取一次,用于"定位到上次那一集的那一秒"。
+11 -5
View File
@@ -139,11 +139,17 @@ class _PlayerPageState extends State<PlayerPage>
if (widget.homeMode &&
widget.feedDramas != null &&
(feedChanged || channelChanged)) {
logic.updateFeed(
widget.feedDramas!,
keepDramaId: channelChanged ? null : logic.currentDramaId,
resetToFirst: channelChanged,
);
final feedDramas = widget.feedDramas!;
final keepDramaId = channelChanged ? null : logic.currentDramaId;
final resetToFirst = channelChanged;
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!mounted) return;
logic.updateFeed(
feedDramas,
keepDramaId: keepDramaId,
resetToFirst: resetToFirst,
);
});
}
logic.onPageUpdated(oldWidget);
}
+35
View File
@@ -1,5 +1,6 @@
// Author: fruit.com.cn
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:fruitshorts/core/data/models/drama.dart';
import 'package:fruitshorts/pages/discover/discover_types.dart';
@@ -121,6 +122,40 @@ void main() {
logic.dispose();
});
testWidgets('should_defer_drama_notification_when_changed_during_build', (
tester,
) async {
final logic = HomeLogic(dramas);
var notifications = 0;
var immediateNotifications = -1;
var changed = false;
logic.addListener(() => notifications++);
await tester.pumpWidget(
MaterialApp(
home: ListenableBuilder(
listenable: logic,
builder: (context, _) {
if (!changed) {
changed = true;
logic.onDramaChanged(dramas[1]);
immediateNotifications = notifications;
}
return const SizedBox.shrink();
},
),
),
);
expect(immediateNotifications, 0);
expect(tester.takeException(), isNull);
expect(logic.currentDrama.id, 'city');
await tester.pump();
expect(notifications, 1);
expect(tester.takeException(), isNull);
logic.dispose();
});
test('should_map_onto_root_tabs_when_my_page_actions_fire', () {
final logic = HomeLogic(dramas);
+63
View File
@@ -236,6 +236,7 @@ void main() {
logic.dispose();
await _drain(logic);
});
});
group('history writes', () {
@@ -618,6 +619,68 @@ void main() {
logic.dispose();
await _drain(logic);
});
testWidgets(
'should_defer_feed_update_side_effects_when_called_during_build',
(tester) async {
final factory = _RecordingFactory();
final initial = [_drama('a'), _drama('b'), _drama('c')];
final latest = [_drama('d'), _drama('c'), _drama('a')];
final logic = _logic(FakeWatchHistory());
logic.attach(
PlayerPage(
drama: initial.first,
initialIndex: 0,
homeMode: true,
feedDramas: initial,
),
isMounted: () => true,
);
await tester.runAsync(() async {
logic.start(
factory: factory.create,
coordinator: PlaybackCoordinator(),
);
await _drain(logic);
});
expect(logic.currentDramaId, 'a');
expect(logic.session.active, isNotNull);
var updateScheduled = false;
await tester.pumpWidget(
MaterialApp(
home: ListenableBuilder(
listenable: logic,
builder: (context, _) {
if (!updateScheduled) {
updateScheduled = true;
logic.updateFeed(latest, keepDramaId: 'a');
}
return Scaffold(
body: PageView.builder(
controller: logic.pages,
itemCount: logic.visibleIndices.length,
itemBuilder: (_, index) =>
ColoredBox(color: Colors.black, key: ValueKey(index)),
),
);
},
),
),
);
expect(tester.takeException(), isNull);
expect(logic.pages.hasClients, isTrue);
await tester.pump();
await tester.pump(const Duration(milliseconds: 100));
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
expect(logic.queue.entryAt(2).drama.id, 'a');
expect(logic.pages.page, closeTo(2, 0.01));
logic.dispose();
},
);
});
group('lifecycle', () {