我的應用程序中的所有屏幕都可以訪問 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] 删除。
我来说两句