I have this function
Future updateMember(Member member) async {
final User? user = Auth().currentUser;
final docMember =
FirebaseFirestore.instance.collection('users').doc(user?.uid);
member.id = docMember.id;
final json = member.toJson();
final response = await docMember
.update(json)
.then((value) => {print("done")})
.catchError((e) => (e));
return response;
}
Then I want to catch the error here and success message here
final response = updateMember(member);
if (response.then((value) => 'done') == true) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('success'),
backgroundColor: Colors.green,
),
);
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(catchError(onError)),
backgroundColor: Colors.red,
),
);
}
Please I need help on how I can implement this
2
Answers
This solves the problem
For this kind of purposes you could use a BLoC pattern, that divide an ui layer and domain layer (communication with server), you can read more on official documentation of bloc library: Bloc library
It might be complicated to a novice in flutter, so, in your case, you can also implement state managment inside a single widget by your own.
In controller’s stream we will add all server responses and work with those by _subscriber;
Whenever you get response from server, you should call _controller.add(response), to add to our stream a new value.
In stream you’l check if code is "OK", then show succes message, otherwise – error.
All code snipped is showed below: