I'm having a bit of a problem with my Dart app. The app is a to-do list creator where the user can create several lists and add items to each one of them.
With this in mind, I started adding functionalities and from a simple, single-list app, now I have created a more complete thing where the user can reorder, edit, and delete items, and the same for lists.
I then organized the lists on a tab system because I thought it looked well. So, for the last part of v1.0.0, I tried to add the "Edit list" functionality, which allows the user to delete a list or edit its name.
The thing is, when I hit "Save" a black screen appears and makes me have to restart the app. The worst thing is that the code works, because the name of the lists changes, but the black screen is unavoidable.
Here are the two widgets the "Edit list" dialog box uses:
//delete or edit list
void confirmDeleteList(String listName) {
try {
setState(() {
db.toDoLists.remove(listName);
tabNames.remove(listName);
if (_tabController.animation!.status == AnimationStatus.completed) {
if (!_isTabControllerDisposed) {
_tabController.dispose(); // Dispose of the old TabController
_isTabControllerDisposed = true;
}
_tabController = TabController(length: db.toDoLists.keys.length, vsync: this);
_isTabControllerDisposed = false;
if (_tabController.length - 1 < _tabController.previousIndex) {
_tabController.index = _tabController.length - 2;
} else {
_tabController.index = _tabController.previousIndex;
}
_currentList = tabNames[_tabController.index]; // Set _currentList to the current tab
}
});
db.updateDatabase();
} catch (e) {
print('Error in confirmDeleteList: $e');
}
}
void deleteOrEditList(String listName) {
try {
showDialog(
context: context,
builder: (context) {
// Create a new TextEditingController for the DeleteListBox
final deleteBoxController = TextEditingController();
return DeleteListBox(
controller: deleteBoxController,
onSaved: () async {
if (deleteBoxController.text.isNotEmpty) {
setState(() {
// Remove the old list and add a new one with the updated name
var items = db.toDoLists.remove(listName);
db.toDoLists[deleteBoxController.text] = items!;
// Update tabNames
tabNames.remove(listName);
tabNames.add(deleteBoxController.text);
if (_tabController.animation!.status == AnimationStatus.completed) {
if (!_isTabControllerDisposed) {
_tabController.dispose(); // Dispose of the old TabController
_isTabControllerDisposed = true;
}
_tabController = TabController(length: db.toDoLists.keys.length, vsync: this);
_isTabControllerDisposed = false;
_tabController.index = tabNames.indexOf(deleteBoxController.text); // Set the renamed tab as the current tab
_currentList = deleteBoxController.text; // Set _currentList to the renamed tab
}
});
db.updateDatabase();
deleteBoxController.clear();
}
Navigator.of(context).pop(); // Close the dialog before disposing the TabController
},
onSlided: () {
confirmDeleteList(listName);
Navigator.of(context).pop(); // Close the dialog before disposing the TabController
return Future.value();
},
tabName: listName,
);
},
);
} catch (e) {
print('Error in deleteList: $e');
}
}
Since I'm kinda new to programming, I don't understand if this block of code is enough to determine the reason for the problem. I've been fighting with this for two days now and I'm starting to get a little frustrated.
I can, of course, provide the rest of the code, if necessary, just let me know :)