1 year ago
#69057

Emil Mammadov
How to handle validation and navigation situation on Cubit right way?
In my application I have AddServerScreen
Page which allows me to add Servers to Cubit. The server object has two properties, alias and address. If the alias of the server to be added matches one of the servers kept in Cubit, I do not add the server and show an error message to the user. But if it's not already added, I add the server to Cubit and go back to the home page.
My question is, Is Cubit supposed to do the validation and Navigator.pop(context)
here? If I do all this in the UI Cubit will have to return Future
from the function and that just doesn't feel right. I need to solve this problem the right way.
I will share simplified ServerState
and ServerCubit
down below.
ServerState:
class ServerState {
final List<Server> servers;
const ServerState(this.servers);
}
ServerCubit:
class ServerCubit extends Cubit<ServerState> {
ServerCubit() : super(const ServerState([]));
void addServer(String address, String alias) {
List<Server> same = state.servers.where((e) => e.alias == alias).toList();
if (same.isEmpty) {
Server server = Server(address, alias);
emit(ServerState([...state.servers, server]));
}
}
}
flutter
bloc
flutter-bloc
cubit
0 Answers
Your Answer