3.1.6 Riverpod 架构优化与 Bug 修复 - #2
Merged
Merged
Conversation
2. 优化了mac平台下的应用名称显示
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Riverpod 架构优化与 Bug 修复
概述
本次优化聚焦于 Riverpod provider 架构的正确性,修复了多个导致"数据重复加载"、"provider 被意外 dispose"、"表单校验不生效"的核心问题,统一了 provider 的使用模式。
核心问题与修复
1. 数据与 UI 状态分离
问题:
BookList.build()同时 watch 了数据源和 UI 状态(选择模式、布局、选中项),导致每次勾选/切换布局都触发整个列表数据重新加载。修复:
BookList.build()移除ref.watch(bookListUiProvider),只 watch 数据源和查询条件ref.watch(bookListUiProvider)获取 UI 状态deleteSelected()从bookListUiProvider读取selectedBookIds涉及文件:
lib/feature/book/ui/provider/book_provider.dartlib/feature/book/ui/view/book_list_view.dart2. FSelectTileGroup 多选修复
问题:使用
FMultiValueControl.managedRadio()(单选)而非managed()(多选),且未传initial导致选中状态丢失。修复:
FMultiValueControl<int>.managed(initial: selectedIds, ...)selectedIds从bookListUiProvider实时获取涉及文件:
lib/feature/book/ui/view/book_list_view.dart3. Family 参数稳定性
问题:在
build()中每次创建新的List<int>作为 family 参数,Dart List 使用引用比较,导致 Riverpod 认为是不同参数 → 旧 provider dispose → 新 provider 创建 → 异步操作中断。修复:将 family 参数在
initState()中创建一次,存为late final字段。涉及文件:
lib/feature/export/ui/view/export_batch_form_view.dart(_bookIds)lib/feature/parse/ui/view/parse_batch_pdf_view.dart(_param)4. Provider build() 生命周期
问题:
build()使用Future.microtask()异步启动工作并立即返回空数据,导致:state = AsyncLoading()与 Riverpod 自动状态管理冲突修复:将解析逻辑直接放入
build()并await,Riverpod 自动管理 loading/data/error 状态。删除所有手动state = AsyncLoading()/AsyncData()/AsyncError()调用。涉及文件:
lib/feature/parse/ui/provider/parse_batch_archive_provider.dartlib/feature/parse/ui/provider/parse_batch_pdf_provider.dart5. Export Provider 防 Dispose
问题:
ExportBatch.build()使用ref.watch(bookListProvider),书籍列表任何变化都导致 export provider 重建/dispose,doExport()异步操作被中断。修复:
build()改用ref.read(bookListProvider)doExport()在每个await后和回调中加ref.mounted守卫涉及文件:
lib/feature/export/ui/provider/export_batch_provider.dartlib/feature/export/ui/provider/export_single_provider.dart6. GlobalKey 放置
问题:
GlobalKey<FormState>在build()中创建(ConsumerWidget),每次 rebuild 都生成新 key,currentState永远为 null,表单校验失效。修复:将 view 改为
ConsumerStatefulWidget,_formKey放在State中。涉及文件:
lib/feature/export/ui/view/export_single_form_view.dartlib/feature/export/ui/view/export_batch_form_view.dart7. readOnly 字段与 Form 校验
问题:
readOnly: true的FTextFormField不参与 Form 的 value 追踪,validate()始终返回 false 但不显示错误,导致"点击无反应"。修复:readOnly 字段不设
validator,在按钮onPress中手动检查 controller 值。涉及文件:
lib/feature/export/ui/view/export_batch_form_view.dart8. showFToast context 失效
问题:删除书籍后 widget 被移除,原
context失效,调用showFToast报错 "Looking up a deactivated widget's ancestor is unsafe"。修复:在异步操作前保存
Navigator.of(context).context(Navigator 级 context,不受 tile 删除影响)。涉及文件:
lib/feature/book/ui/view/book_list_view.dart(3 处)9. 保存进度 total 始终为 0
问题:
saveBatchAsBook()的回调只更新current,从未设置total。修复:在
await前初始化 progress provider,设置total: parseBatchList.length。涉及文件:
lib/feature/parse/ui/provider/parse_batch_pdf_provider.dart关键模式总结
ref.read避免级联重建initState()创建一次,不要每次 build 新建await工作,让 Riverpod 管理 loading/data/errorConsumerStatefulWidget的State中Navigator.of(context).contextref.mounted(provider)/context.mounted(widget)validator,手动检查 controller