How can I handle the errors and show the loading progress of the data upload. I am new on flutter.
floatingActionButton: FloatingActionButton(
onPressed: () async {
newHome = Home(
urlPhotohome: urlPhotohome,
urlPhotoStreet: urlPhotoStreet,
dateTime: DateTime.now(),
isFavorite: false);
await FirebaseFirestore.instance
.collection('city')
.doc(userId)
.collection('home')
.add(newHome.toMap());
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text("Home save"),
duration: Duration(milliseconds: 900),
),
);
},
backgroundColor: Colors.blueGrey,
child: const Icon(Icons.save),
),
CodePudding user response:
Use try-catch.
try {
//show progressbar
await FirebaseFirestore.instance
.collection('city')
.doc(userId)
.collection('home')
.add(newHome.toMap());
//hide progress bar
} on FirebaseException catch (e) {
// Caught an exception from Firebase.
//hide progress bar
print("Failed with error '${e.code}': ${e.message}");
}