Skip to content

Global key is getting reused in navigator #10154

Closed

Description

@123qws

This is happening when I pop the route and then push a new route which uses as same global key in the old route.

Here is a simple example:

import 'package:flutter/material.dart';

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new GestureDetector(
      onTap:() {
        Navigator.push(context, new MaterialPageRoute(builder: (_) => new Company(0)));
      },
      child: new Text('go to company'));
  }
}

class Company extends StatelessWidget {
  static final _scaffoldKey = new GlobalKey<ScaffoldState>();
  final int companyId;
  Company(this.companyId);

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      key: _scaffoldKey,
      body: new Column(children: [
        new Text('Company $companyId'),
        new GestureDetector(
          onTap:() {
            Navigator.pop(context);
            Navigator.push(context,
              new MaterialPageRoute(builder: (_) => new Company(companyId+1)));
          },
          child: new Text('next company')),
      ]
    ));
  }
}

Clicks "go to company" and then clicks "next company", you will see

I/flutter ( 8026): The following assertion was thrown building Company():
I/flutter ( 8026): Multiple widgets used the same GlobalKey.
I/flutter ( 8026): The key [LabeledGlobalKey<ScaffoldState>#33415169] was used by multiple widgets. The parents of
I/flutter ( 8026): those widgets were different widgets that both had the following description:
I/flutter ( 8026):   Company()
I/flutter ( 8026): A GlobalKey can only be specified on one widget at a time in the widget tree.

It seems that the popped route did not remove the global key from the widget tree.

Activity

  1. eseidelGoogle commented on May 18, 2017

    @eseidelGoogle
    Contributor

    @Hixie would know.

  2. Hixie commented on May 18, 2017

    @Hixie
    Contributor

    When you pop a route, it animates away. While this is happening, the old route and the new route are both on the screen at the same time, and cannot share a global key.

  3. 123qws commented on May 18, 2017

    @123qws
    ContributorAuthor

    Thanks @Hixie !

    The issue is resolved if you make _scaffoldKey non-static. I was wondering is this a proper way to assign a global key to the StatelessWidget or State<T> for StatefulWidget?

    class CompanyState extends State<Company> {
       final _scaffoldKey = new GlobalKey<ScaffoldState>();
       /// use _scaffoldKey to show snackbar... 
    }
  4. Hixie commented on May 18, 2017

    @Hixie
    Contributor

    Usually I just skip using the key and use the Builder pattern instead. See the second code snippet here:
    https://docs.flutter.io/flutter/material/Scaffold/of.html

  5. 123qws commented on May 18, 2017

    @123qws
    ContributorAuthor

    Thanks! Builder pattern looks good!

  6. added a commit that references this issue on Sep 12, 2019
  7. added a commit that references this issue on Sep 13, 2019
  8. added a commit that references this issue on Sep 13, 2019
  9. added 2 commits that reference this issue on Sep 30, 2019
  10. github-actions commented on Sep 4, 2021

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  11. locked as resolved and limited conversation to collaborators on Sep 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions