Firebase task.snapshotEvents.listen StateProvider 未獲得更新的上傳進度

你自己

我的應用程序中的所有屏幕都可以訪問 Uploading State Provider。當我從上傳屏幕上傳對象時,我使用FlutterFire 處理任務來獲取上傳進度。

上傳任務工作正常,但是當我從上傳屏幕導航到主屏幕時,我正在顯示上傳 StateProvider 的進度,它只顯示當前進度,而不會在後台進行更新。

我如何配置 RiverPod 以偵聽仍在運行的後台快照,還是應該避免為此使用 RiverPod 而僅使用 Firebase 存儲偵聽器?

上傳狀態提供者

final uploadingStateProvider = StateProvider<String?>((ref) => null);

Firebase 存儲上傳任務正在向我的 StateProvider 發送進度字符串

task.snapshotEvents.listen((TaskSnapshot snapshot) {
  if (displayProgressNotification) {
    final int percent =
        ((snapshot.bytesTransferred / snapshot.totalBytes) * 100).round();
    if (percent >= 100) {
      context.read(uploadingStateProvider).state = null;
    } else {
      context.read(uploadingStateProvider).state =
          percent.toString();
    }
  } else {
    context.read(uploadingStateProvider).state = null;
  }
}, onDone: () async {
  context.read(uploadingStateProvider).state = null;
  if (deleteAfterUpload) {
    await file.delete();
  }
}, onError: (e) {
  context.read(uploadingStateProvider).state = null;
  debugPrint(task.snapshot.toString());
  debugPrint(e.toString());
});

主屏幕顯示帶有百分比的進度微調器,但它只顯示它讀取的初始百分比並且不會從後台任務中獲取更新的百分比

Consumer(
  builder: (context, watch, child) {
    final uploadingPercent =
        watch(uploadingStateProvider).state;
    if (uploadingPercent != null) {
      return Stack(
        alignment: Alignment.center,

        /// Note: First child is the bottom of the stack...
        children: [
          SpinKitDualRing(
            color: Colors.white.withOpacity(0.5),
            size: titleFontSize(shrinkOffset),
            lineWidth: 2.0,
            duration: const Duration(
                milliseconds: 1500),
          ),
          Text(
            '$uploadingPercent%',
            style: TextStyle(
              color: Colors.white.withOpacity(0.5),
              fontSize: uploadFontSize(shrinkOffset),
              fontWeight: FontWeight.w200,
            ),
          ),
        ],
      );
    } else {
      return const SizedBox.shrink();
    }
  },
),
馬里奧·弗朗索瓦

我會使用 1.0.0 版本。沒有上下文

final uploadingStateProvider =
    StateNotifierProvider.autoDispose<UploadingState, int?>((ref) {
  return UploadingState(read: ref.read);
});

我會使用 StateNotifier。如果您想要 myUploadTask 方法中的整個邏輯,閱讀器會很有用。

class UploadingState extends StateNotifier<int?> {
  final Reader read;
  StreamSubscription? mySubscription;

  UploadingState({required this.read}) : super(null);

  Future<void> myUploadTask(firebase_storage.UploadTask task) async {
    mySubscription = task.snapshotEvents.listen((TaskSnapshot snapshot) {
      final int percent =
          ((snapshot.bytesTransferred / snapshot.totalBytes) * 100).round();
      state = percent;
    }, onDone: () async {
      state = null;
    }, onError: (e) {
      state = null;
      debugPrint(task.snapshot.toString());
      debugPrint(e.toString());
    });
  }

  @override
  void dispose() {
    mySubscription?.cancel();
    super.dispose();
    
  }
}

還有你的消費者

Consumer(
  builder: (context,  ref,_) {
    final uploadingPercent = ref.watch(uploadingStateProvider);
    if (uploadingPercent != null) {
      return Stack(
        alignment: Alignment.center,

        /// Note: First child is the bottom of the stack...
        children: [
          SpinKitDualRing(
            color: Colors.white.withOpacity(0.5),
            size: titleFontSize(shrinkOffset),
            lineWidth: 2.0,
            duration: const Duration(
                milliseconds: 1500),
          ),
          Text(
            '$uploadingPercent%',
            style: TextStyle(
              color: Colors.white.withOpacity(0.5),
              fontSize: uploadFontSize(shrinkOffset),
              fontWeight: FontWeight.w200,
            ),
          ),
        ],
      );
    } else {
      return const SizedBox.shrink();
    }
  },
),

在某處調用它或將所有邏輯放在 UploadingState() 中

ref.read(uploadingStateProvider.notifier).myUploadTask(task);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

否则在StateProvider上

當我將文件上傳到 Android 中的 Firebase 存儲時,進度條未更新

使用Firebase函数使用.listen()

(Flutter/Riverpod) StateProvider 文檔示例未更新

如何在同一线程上继续Firebase Task

Firebase createUserWithEmailAndPassword task.isSuccessful()

无法调用$ stateProvider,两次更新视图(它仅在第一次工作)

测试stateProvider状态(如果在angularJS的config中配置)在$ state上返回null

Karma上的AngularJS未知提供程序错误,其值由$ stateProvider解析

在Mediawiki上安装{{listen}}模板?

允许从stateProvider进入视图

AngularJS-动态$ StateProvider

Firebase 上傳保護

使用$ stateProvider的AngularJS /离子路由-控制器在第二次调用状态时未重新加载

ui-router $ stateProvider文本参数未与ui-view,AngularJS 1.6.x一起显示

AngularJS中的控制器不起作用。我使用$ StateProvider。ASP.NET-Core上的项目

Firebase 变量未更新

在stateprovider中填充嵌套视图

将Firebase Task <Void>包装到RxJava CompletableEmitter中?

List<Task> 上的 Task.WhenAll 与 IEnumerable<Task> 上的 Task.WhenAll 的行为不同

如何在Java Spring Boot的firebase存儲中獲取上傳文件的downloadURL

Firebase崩溃,“针对同一查询两次调用listen()”错误

Firebase Pod更新未更新

文件上傳到瀏覽器時如何獲取進度條

错误:StateProvider(...):渲染未返回任何内容。这通常意味着缺少return语句。或者,不渲染任何内容,则返回null

Firebase ListView 未实时更新

Firebase数据未正确更新

Firebase / Firestore文档未更新

Firebase数据更改未更新