@@ -772,11 +772,33 @@ class _AnnotationContentViewState extends ConsumerState<AnnotationContentView> {
772772 .watch (learningSettingsProvider)
773773 .autoExpandCachedAnnotation;
774774 // watch 共享字幕投影:撑起其 autoDispose 生命周期(视图在屏则常驻)+ 同步取前后句。
775+ // 翻译缓存 key 包含前后句,必须等字幕投影就绪后才允许自动翻译;
776+ // 否则首帧会用 null/null 写入无上下文 key,返回页面时再用带上下文 key 读取就会 miss。
775777 final audioItemId = widget.audioItemId;
776- final sentences = (audioItemId != null && audioItemId.isNotEmpty)
777- ? ref.watch (audioSentencesProvider (audioItemId)).valueOrNull
778+ final hasSentenceContextSource =
779+ audioItemId != null &&
780+ audioItemId.isNotEmpty &&
781+ widget.sentenceIndex != null ;
782+ final sentencesAsync = (audioItemId != null && audioItemId.isNotEmpty)
783+ ? ref.watch (audioSentencesProvider (audioItemId))
778784 : null ;
779- final (previousText, nextText) = _neighborTexts (sentences);
785+ final translationContextReady =
786+ ! hasSentenceContextSource || (sentencesAsync? .hasValue ?? false );
787+ final sentences = translationContextReady
788+ ? sentencesAsync? .valueOrNull
789+ : null ;
790+ final (previousText, nextText) = translationContextReady
791+ ? _neighborTexts (sentences)
792+ : (null , null );
793+
794+ Future <(String ?, String ?)> resolveTranslationContext () async {
795+ if (! hasSentenceContextSource) return (null , null );
796+ final loadedSentences =
797+ sentencesAsync? .valueOrNull ??
798+ await ref.read (audioSentencesProvider (audioItemId).future);
799+ return _neighborTexts (loadedSentences);
800+ }
801+
780802 final cachedTranslation = autoExpand
781803 ? ai
782804 ? .getCachedTranslation (
@@ -889,12 +911,14 @@ class _AnnotationContentViewState extends ConsumerState<AnnotationContentView> {
889911 ? (cancelToken, source) async * {
890912 var hasContent = false ;
891913 try {
914+ final (requestPreviousText, requestNextText) =
915+ await resolveTranslationContext ();
892916 // await for(非 yield*)确保流内 auth/quota 错误在本
893917 // try 内重抛,从而弹登录/订阅(与 onRequestAnalysis 语义一致)。
894918 await for (final t in ai.getTranslationStream (
895919 widget.text,
896- previous: previousText ,
897- next: nextText ,
920+ previous: requestPreviousText ,
921+ next: requestNextText ,
898922 targetLanguage: nativeLanguage,
899923 accessToken: accessToken,
900924 cancelToken: cancelToken,
@@ -975,7 +999,8 @@ class _AnnotationContentViewState extends ConsumerState<AnnotationContentView> {
975999 : null ,
9761000 cachedTranslation: cachedTranslation,
9771001 cachedAnalysis: cachedAnalysis,
978- autoLoadTranslation: shouldAutoLoadSentenceAi,
1002+ autoLoadTranslation:
1003+ shouldAutoLoadSentenceAi && translationContextReady,
9791004 autoLoadAnalysis: shouldAutoLoadSentenceAi,
9801005 onTranslationUserIntent: () {
9811006 ref
0 commit comments