i have a basic flutter app with a bottom nav bar following the docs im adding aws amplify auth and using amplify_authenticator ^1.4.2 package
in the docs it says if you want any of your views to check for authentication wrap it with AuthenticatedView widget but i get this error Duplicate GlobalKey detected in widget tree. when switching between 2 tabs that are wrapped with AuthenticatedView widget
my initial screen can be viewed by anyone they don't need auth but if they switch to any other tabs they will need to be authenticated as i have wrapped it with AuthenticatedView
this is my bottom nav bar setup
@RoutePage()
class InitialScreen extends StatelessWidget {
InitialScreen({super.key});
@override
Widget build(BuildContext context) {
return AutoTabsRouter.tabBar(
routes: const [
HomeRoute(),
MyWorkoutsRoute(),
ChatRoute(),
SettingsRoute()
],
builder: (context, child, controller) {
final tabsRouter = AutoTabsRouter.of(context);
return Scaffold(
appBar: _buildAppBar(context, tabsRouter),
body: child,
bottomNavigationBar: _buildBottomNavigationBar(tabsRouter),
);
},
);
}
// just gives the tabs names
final Map<String, String> routeTitles = {
HomeRoute.name: 'Make me',
SelectBodyPartRoute.name: 'Muscle',
MyWorkoutsRoute.name: 'Workouts',
ChatRoute.name: 'Chat',
SettingsRoute.name: 'Settings',
WorkoutRoute.name: 'Workout'
};
AppBar _buildAppBar(BuildContext context, TabsRouter tabsRouter) {
return AppBar(
systemOverlayStyle: SystemUiOverlayStyle.dark,
backgroundColor: Colors.teal,
title: Text(
routeTitles[context.topRoute.name] ?? ''),
leading: const AutoLeadingButton(),
iconTheme: const IconThemeData(
color: Colors.black,
),
);
}
//GNAV just a package for navbar customisation
Widget _buildBottomNavigationBar(TabsRouter tabsRouter) {
return SafeArea(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: GNav(
onTabChange: (index) {
tabsRouter.setActiveIndex(index);
},
tabBackgroundColor: Colors.black87,
tabs: [
_buildNavButton(Icons.home, 'Home'),
_buildNavButton(Icons.fitness_center_sharp, 'Workouts'),
_buildNavButton(Icons.mail_outline, 'Chat'),
_buildNavButton(Icons.settings, 'Settings'),
],
),
),
);
}
GButton _buildNavButton(IconData icon, String text) {
return GButton(
icon: icon,
text: text,
textStyle: const TextStyle(
fontSize: 20,
color: Colors.teal,
fontWeight: FontWeight.bold,
),
);
}
}
the error happens when i tab from this screen
@RoutePage()
class MyWorkoutsScreen extends StatelessWidget {
const MyWorkoutsScreen({super.key});
@override
Widget build(BuildContext context) {
return AuthenticatedView(child: const Placeholder());
}
}
to this screen
@RoutePage()
class ChatScreen extends StatelessWidget {
const ChatScreen({super.key});
@override
Widget build(BuildContext context) {
return const AuthenticatedView(child: Placeholder());
}
}
this is the error that gets printed out
The following assertion was thrown while finalizing the widget tree:
Duplicate GlobalKey detected in widget tree.
The following GlobalKey was specified multiple times in the widget tree. This will lead to parts of the widget tree being truncated unexpectedly, because the second time a key is seen, the previous instance is moved to the new location. The key was:
- [LabeledGlobalKey<ScaffoldMessengerState>#36932]
This was determined by noticing that after the widget with the above global key was moved out of its previous parent, that previous parent never updated during this frame, meaning that it either did not update at all or updated before the widget was moved, in either case implying that it still thinks that it should have a child with that global key.
The specific parent that did not update after having one or more children forcibly removed due to GlobalKey reparenting is:
- Directionality(textDirection: ltr)
A GlobalKey can only be specified on one widget at a time in the widget tree.
When the exception was thrown, this was the stack
#0 BuildOwner.finalizeTree.<anonymous closure>
framework.dart:3109
#1 BuildOwner.finalizeTree
framework.dart:3134
#2 WidgetsBinding.drawFrame
binding.dart:919
#3 RendererBinding._handlePersistentFrameCallback
binding.dart:360
#4 SchedulerBinding._invokeFrameCallback
binding.dart:1297
#5 SchedulerBinding.handleDrawFrame
binding.dart:1227
#6 SchedulerBinding._handleDrawFrame
binding.dart:1085
#7 _invoke (dart:ui/hooks.dart:170:13)
#8 PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:401:5)
#9 _drawFrame (dart:ui/hooks.dart:140:31)
i know its to do with AuthenticatedView because when i remove it i dont get any errors it only happens when i tab from one AuthenticatedView to another view that is also a AuthenticatedView
when i tab from my homescreen which is not authenticated to ChatScreen and back i get no errors
not sure how to fix it as im very new aws amplify and not long been learning flutter any help appreciated