当用户脱机时,如何在Flatter中向firebase添加/上载数据时显示错误?
基本上,当用户尝试添加文档或将图像上载到firestore cloud/firebase存储时,我尝试显示一个警报对话框。我用try-and-catch块以及.catchError()尝试了这个方法,但它不起作用,基本上是在连接到internet后进行的。我想做的是显示一个警报对话框并取消该过程,即使用户恢复了连接(他必须再次重复上传过程)。以下是我的代码示例: 这用于拾取和上载图像:当用户脱机时,如何在Flatter中向firebase添加/上载数据时显示错误?,firebase,flutter,google-cloud-firestore,firebase-storage,Firebase,Flutter,Google Cloud Firestore,Firebase Storage,基本上,当用户尝试添加文档或将图像上载到firestore cloud/firebase存储时,我尝试显示一个警报对话框。我用try-and-catch块以及.catchError()尝试了这个方法,但它不起作用,基本上是在连接到internet后进行的。我想做的是显示一个警报对话框并取消该过程,即使用户恢复了连接(他必须再次重复上传过程)。以下是我的代码示例: 这用于拾取和上载图像: Future _getImage(bool isCamera) async { var picked
Future _getImage(bool isCamera) async {
var pickedImage;
try {
if (isCamera)
pickedImage =
await picker.getImage(source: ImageSource.camera, imageQuality: 20);
else
pickedImage = await picker.getImage(
source: ImageSource.gallery, imageQuality: 20);
if (pickedImage != null) {
_uploadAndSetUrl(File(pickedImage.path));
}
} catch (e) {
print('error here');
}
}
Future _uploadAndSetUrl(File image) async {
try {
_uploadingStatus(true);
StorageUploadTask uploadTask = _bookImageRef.putFile(image);
StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
String downloadAddress = await _bookImageRef.getDownloadURL();
_uploadingStatus(false);
setState(() {
_imageUrl = downloadAddress;
});
print(_imageUrl);
} on PlatformException catch (e) {
print('error: $e');
} catch (e) {
print('No image added');
}
}
还有另一种方法可以将文档添加到firestore云中。有办法吗
提前谢谢你 试试这个
import 'dart:io';
Future _getImage(bool isCamera) async {
var pickedImage;
try {
if (isCamera)
pickedImage =
await picker.getImage(source: ImageSource.camera, imageQuality: 20);
else
pickedImage = await picker.getImage(
source: ImageSource.gallery, imageQuality: 20);
if (pickedImage != null) {
try {
final result = await InternetAddress.lookup('google.com');
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty)
_uploadAndSetUrl(File(pickedImage.path));
} on SocketException catch (_) {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text("Connection Error !"),
content: Text("Please connect to the internet."),
)
);
}
}
} catch (e) {
print('error here');
}
}
如果有帮助,请告诉我