'Fix' for GlobalKey error #254

This commit is contained in:
Imran Remtulla
2023-02-04 12:30:34 -05:00
parent eef4d33431
commit 71c80f11f5

View File

@@ -63,21 +63,29 @@ class _HomePageState extends State<HomePage> {
.map((e) => .map((e) =>
NavigationDestination(icon: Icon(e.icon), label: e.title)) NavigationDestination(icon: Icon(e.icon), label: e.title))
.toList(), .toList(),
onDestinationSelected: (int index) { onDestinationSelected: (int index) async {
HapticFeedback.selectionClick(); HapticFeedback.selectionClick();
setState(() { if (index == 0) {
if (index == 0) { while ((pages[0].widget.key as GlobalKey<AppsPageState>)
.currentState !=
null) {
// Avoid duplicate GlobalKey error
await Future.delayed(const Duration(milliseconds: 200));
}
setState(() {
selectedIndexHistory.clear(); selectedIndexHistory.clear();
} else if (selectedIndexHistory.isEmpty || });
(selectedIndexHistory.isNotEmpty && } else if (selectedIndexHistory.isEmpty ||
selectedIndexHistory.last != index)) { (selectedIndexHistory.isNotEmpty &&
selectedIndexHistory.last != index)) {
setState(() {
int existingInd = selectedIndexHistory.indexOf(index); int existingInd = selectedIndexHistory.indexOf(index);
if (existingInd >= 0) { if (existingInd >= 0) {
selectedIndexHistory.removeAt(existingInd); selectedIndexHistory.removeAt(existingInd);
} }
selectedIndexHistory.add(index); selectedIndexHistory.add(index);
} });
}); }
}, },
selectedIndex: selectedIndex:
selectedIndexHistory.isEmpty ? 0 : selectedIndexHistory.last, selectedIndexHistory.isEmpty ? 0 : selectedIndexHistory.last,