This is less about a specific implementation, but more about good practice.
I have the following structure in a flutter desktop project:
- DataProviders: read data from one of two different fileformats (locally)
- Repository: parses the data and instantiates my Model
- ProjectCubit: takes a path from a FilePicker and gets the Project from the upper 2 layers
ProjectCubit.dart:
class ProjectCubit extends Cubit<ProjectState> {
ProjectCubit() : super(ProjectState.Closed);
Project? loadedProject;
Project? getProject() {
// return loaded instance of Project if loaded
if(loadedProject != null)
return loadedProject;
}
// creates Project instance from csv file
void importProject(String filePath) async {
emit(ProjectState.Importing);
loadedProject = await ProjectRepository().loadData(loadType: ProjectLoadType.IMPORT_FROM_CSV, filePath: filePath);
emit(ProjectState.Open);
}
// open json-Project file
void openProject(String filePath) async {
emit(ProjectState.Opening);
try {
loadedProject = await ProjectRepository().loadData(loadType: ProjectLoadType.OPEN_PROJECT_FILE, filePath: filePath);
} catch (e) {
emit(ProjectState.Closed);
Log().l.e("Opening file failed with ${e.toString()}");
}
emit(ProjectState.Open);
}
}
where the states are:
enum ProjectState {
Closed,
Importing,
Opening,
Open
}
The Project instance in the ProjectCubit needs to be accessed and changed from multiple screens in multiple settings (DataTable, simple Inputs etc.). For example, Project has a Customer, which has a customerName, customerId etc. which have to be changed from a Customer-Settings screen.
I thought of two ways:
- creating a ProjectSettingsCubit, CustomerDataCubit, ProjectDataCubit etc. which take the ProjectCubit as an argument and modify the Project from there
- just using the ProjectCubit the entire time and making the changes from the Presentation layer
What would be the best way to accomplish this? And if the whole structure or Cubit is bad, why?
Would appreciate any help, thanks