When I go from screen A to screen B or C for the first time it works fine, but when I go back to screen A and open again screen B or C it crashes showing the following message:
════════ Exception caught by widgets library ═══════════════════════════════════
'package:flutter/src/widgets/will_pop_scope.dart': Failed assertion: line 59 pos 12: '_route == ModalRoute.of(context)': is not true.
The relevant error-causing widget was
Form-[LabeledGlobalKey#ce623 L]
lib\…\ui\LoginScreen.dart:73
════════════════════════════════════════════════════════════════════════════════
════════ Exception caught by widgets library ═══════════════════════════════════
'package:flutter/src/widgets/framework.dart': Failed assertion: line 4973 pos 14: '_dependents.isEmpty': is not true.
The relevant error-causing widget was
MaterialApp
lib\main.dart:36
════════════════════════════════════════════════════════════════════════════════
I/flutter ( 7224): Onboard
════════ Exception caught by widgets library ═══════════════════════════════════
Duplicate GlobalKeys detected in widget tree.
═══════════════════════════════════════════════════════════════════════════════
If you could share the code snippet then we would be better able to solve this issue.
import 'package:flutter/material.dart';
import 'package:teste/Screen1.dart';
import 'package:teste/Screen2.dart';
void main() {
runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
home: MyHomePage(title: 'Flutter Demo Home Page'),
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
GlobalKey<ScaffoldState> _globalKeyScaffold = GlobalKey<ScaffoldState>();
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
key: _globalKeyScaffold,
backgroundColor: Colors.red,
body: Center(
child: Stack(
children: <Widget>[
Hero(
tag: "placeholder",
child: Placeholder()),
Center(child:
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Hero(
tag: "screen1",
child: RaisedButton(onPressed: (){
Navigator.push(context, MaterialPageRoute(builder: (context)=>Screen1()));
}, child: Text("Screen1"),),
Hero(
tag: "screen2",
child: RaisedButton(onPressed: (){
Navigator.push(context, MaterialPageRoute(builder: (context)=>Screen2()));
}, child: Text("Screen2"),)),
Screen1.dart
import 'package:flutter/material.dart';
class Screen1 extends StatefulWidget {
@override
_Screen1State createState() => _Screen1State();
GlobalKey<ScaffoldState> _globalKeyScaffold1 = GlobalKey<ScaffoldState>();
GlobalKey<FormState> _formKey1 = GlobalKey<FormState>(debugLabel: "1");
class _Screen1State extends State<Screen1> {
@override
Widget build(BuildContext context) {
return Scaffold(
key: _globalKeyScaffold1,
backgroundColor: Colors.blue,
body: Stack(
alignment: Alignment.bottomLeft,
children: <Widget>[
Hero(
tag: "placeholder",
child: Placeholder()),
Hero(
tag: "screen1",
child: Container(
color: Colors.white,
height: MediaQuery.of(context).size.height/2,
width: MediaQuery.of(context).size.width-100,
child: Form(
key: _formKey1,
child: Column(children: <Widget>[
TextFormField(
validator: (string){
if(string.length<5){
return "Ok";
TextFormField(),
TextFormField(),
RaisedButton(onPressed: (){
_formKey1.currentState.validate();
],)),),
Screen2.dart
import 'package:flutter/material.dart';
class Screen2 extends StatefulWidget {
@override
_Screen2State createState() => _Screen2State();
GlobalKey<ScaffoldState> _globalKeyScaffold2 = GlobalKey<ScaffoldState>();
GlobalKey<FormState> _formKey2 = GlobalKey<FormState>(debugLabel: "2");
class _Screen2State extends State<Screen2> {
@override
Widget build(BuildContext context) {
return Scaffold(
key: _globalKeyScaffold2,
backgroundColor: Colors.green,
body: Stack(
alignment: Alignment.bottomRight,
children: <Widget>[
Hero(
tag: "placeholder",
child: Placeholder()),
Hero(
tag: "screen2",
child: Container(
color: Colors.white,
height: MediaQuery.of(context).size.height/2,
width: MediaQuery.of(context).size.width-100,
child: Form(
key: _formKey2,
child: Column(children: <Widget>[
TextFormField(
validator: (string){
if(string.length<5){
return "Ok";
TextFormField(),
TextFormField(),
RaisedButton(onPressed: (){
_formKey2.currentState.validate();
],)),),
flutter doctor (when isn't working)
[√] Flutter (Channel stable, v1.17.5, on Microsoft Windows [versão 10.0.18362.900], locale pt-BR)
[√] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
[√] Android Studio (version 3.5)
[√] VS Code, 64-bit edition (version 1.46.1)
[√] Connected device (1 available)
• No issues found!
flutter doctor (working)
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.17.3, on Mac OS X 10.14.6 18G103, locale pt-BR)
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
[✓] Xcode - develop for iOS and macOS (Xcode 11.3.1)
[✓] Android Studio (version 3.5)
[✓] VS Code (version 1.46.1)
[!] Connected device
! No devices available
! Doctor found issues in 1 category.
(On Windows also work when I downgrade do 1.14.6 and on Mac also doesn't work when I update to 1.17)
waiting for customer response
The Flutter team cannot make further progress on this issue until the original reporter responds
label
Jul 9, 2020
Hi @equisbacon,
I looked at your code and observed that the below code snippet is causing the issue you are seeing:
child: Form(
key: _formKey1,
child: Column(children: <Widget>[
TextFormField(
validator: (string){
if(string.length<5){
return "Ok";
return null;
TextFormField(),
TextFormField(),
RaisedButton(onPressed: (){
_formKey1.currentState.validate();
],)),
For some reason, when you go back from screen 1, the screen (screen 1) isn't disposed and hence the _globalKeyScaffold1
still is in cache and when are on main screen (ie the screen which shows screen1 and screen2), the _globakeyScaffold
is detected along with _globalKeyScaffold1
and hence the error 'duplicate globalkeys detected
.
I think this seem to be a coding issue wherein you'll need to dispose the state of the screens when they navigate to and fro.
You may also get some help if you post it on Stack Overflow and if you need help with your code, please see https://www.reddit.com/r/flutterhelp/
Closing, as this isn't an issue with Flutter itself. If you disagree, please write in the comments and I will reopen it.
Thank you