'Multiple widgets used the same GlobalKey
I'm using Flutter. I have a simple app with 3 tabs. There is a RefreshIndicator in each tab with a ListView. The rows are built in another method. This is the code:
@override
Widget build(BuildContext context) {
final GlobalKey<RefreshIndicatorState> _RIKey1 = new GlobalKey<RefreshIndicatorState>();
final GlobalKey<RefreshIndicatorState> _RIKey2 = new GlobalKey<RefreshIndicatorState>();
final GlobalKey<RefreshIndicatorState> _RIKey3 = new GlobalKey<RefreshIndicatorState>();
debugPrint(_RIKey1.toString());
debugPrint(_RIKey2.toString());
debugPrint(_RIKey3.toString());
return new Scaffold(
body: new DefaultTabController(
length: 3,
child: new Scaffold(
appBar: new AppBar(
bottom: new TabBar(
tabs: [
new Tab(icon: new Icon(Icons.view_list)),
new Tab(icon: new Icon(Icons.hotel)),
new Tab(icon: new Icon(Icons.assessment)),
],
),
title: new Text('Data'),
),
body: new TabBarView(
children: [
new RefreshIndicator(
key: _RIKey1,
onRefresh: _actualizoData,
child: new ListView.builder(
padding: new EdgeInsets.only(top: 5.0),
itemCount: linea_reservas.length * 2,
itemBuilder: (BuildContext context, int position) {
if (position.isOdd) return new Divider();
final index = position ~/ 2;
return _buildRow(index);
}),
),
new RefreshIndicator(
key: _RIKey2,
onRefresh: _actualizoData,
child: new ListView.builder(
padding: new EdgeInsets.only(top: 8.0),
itemCount: linea_inouthouse.length * 2,
itemBuilder: (BuildContext context, int position) {
if (position.isOdd) return new Divider();
final index = position ~/ 2;
return _buildRowInOutHouse(index);
}),
),
new RefreshIndicator(
key: _RIKey3,
onRefresh: _actualizoData,
child: new ListView.builder(
padding: new EdgeInsets.only(top: 5.0),
itemCount: linea_ocupacion.length * 2,
itemBuilder: (BuildContext context, int position) {
if (position.isOdd) return new Divider();
final index = position ~/ 2;
return _buildRowOcupacion(index);
}),
),
],
),
),
),
);
}
I'd added the debugPrints and the output are 6 lines, instead of 3.
I/flutter ( 5252): [LabeledGlobalKey<RefreshIndicatorState>#4d76c]
I/flutter ( 5252): [LabeledGlobalKey<RefreshIndicatorState>#59b9e]
I/flutter ( 5252): [LabeledGlobalKey<RefreshIndicatorState>#2c88b]
I/flutter ( 5252): [LabeledGlobalKey<RefreshIndicatorState>#7bd42]
I/flutter ( 5252): [LabeledGlobalKey<RefreshIndicatorState>#1c984]
I/flutter ( 5252): [LabeledGlobalKey<RefreshIndicatorState>#dbe20]
the app works, but after changing tabs a few times, it crashes with this error:
I/flutter ( 5252): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 5252): The following assertion was thrown building NotificationListener<KeepAliveNotification>:
I/flutter ( 5252): Multiple widgets used the same GlobalKey.
I/flutter ( 5252): The key [LabeledGlobalKey<RefreshIndicatorState>#7bd42] was used by multiple widgets. The parents of
I/flutter ( 5252): those widgets were:
I/flutter ( 5252): - RepaintBoundary-[<[LabeledGlobalKey<RefreshIndicatorState>#7bd42]>](renderObject:
I/flutter ( 5252): RenderRepaintBoundary#60a4a DETACHED)
I/flutter ( 5252): - RepaintBoundary-[<[LabeledGlobalKey<RefreshIndicatorState>#7bd42]>](renderObject:
I/flutter ( 5252): RenderRepaintBoundary#c8cdb NEEDS-LAYOUT NEEDS-PAINT)
I/flutter ( 5252): A GlobalKey can only be specified on one widget at a time in the widget tree.
The keys are generated in the Build method, so, I don't understand why the Multiple widgets used the same GlobalKey error
Why the key is generated again, and why it's not unique? I'm not talking of a thousand intents, the error appears after changing between tabs 4 or 5 times. Thank you for any help.
Solution 1:[1]
this code worked for me ... I want to create a List of Form Key ...
just Changing GlobalKey with GlobalObjectKey
like below.
final List<GlobalObjectKey<FormState>> formKeyList = List.generate(10, (index) => GlobalObjectKey<FormState>(index));
Solution 2:[2]
For those who need to continue with GlobalKey:
import 'package:flutter/widgets.dart';
class JosKeys {
static final josKeys1 = GlobalKey();
static final josKeys2 = GlobalKey();
}
Using as follows:
[
CoachTutorialModel(
ids: "post",
globalKeys: JosKeys.josKeys1,
icons: Icon(
Icons.message,
color: Colors.white,
size: 90,
),
...
),
CoachTutorialModel(
ids: "post2",
globalKeys: JosKeys.josKeys2,
icons: Icon(
Icons.message,
color: Colors.white,
size: 90,
),
...
),
]
Solution 3:[3]
We can solve this problem in different ways by its definition and requirement:
Just use this way to resolve the problem
@override
Widget build(BuildContext context) {
//... return
new RefreshIndicator(key: new GlobalKey<RefreshIndicatorState>(), ...
}
Using custom debugLabel
Changing the key from static final to just final and adding debugLabel solved it
class _LoginFormState extends State<yourStatefulWidget> {
final GlobalKey<FormState> _formKey =
new GlobalKey<FormState>(debugLabel: '_LoginFormState');
...
Create a key with final
Changing from static to non-static means the key is being recreated every time the widget is, so it's no longer global. This defeats its purpose.
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
Change the key to refKey
Suppose you using a custom widget that wrapped a Container widget. needed to pass the key from the custom widget to the container, and using a parameter key in the wrapper constructor. Ran into the same issue. fix to avoid using the word key in the wrapper constructor, changed it to refKey
Container(
refKey: …,
)
Solution 4:[4]
In my case I was adding a key to a widget inside a method that was called in multiple places, the code was not mine so it took a while to debug, just gonna leave this here maybe it helps someone.
Solution 5:[5]
I had this issue in my flutter project due to Circular dependency. Please check if Widget A imported Widget B, and Widget B imported Windget A
Solution 6:[6]
I was using a custom widget that wrapped a Container widget. I needed to pass the key from the custom widget to the container, and I was using a parameter key in the wrapper constructor. Ran into the same issue. My fix was to avoid using the word key in the wrapper constructor, changed it to refKey and the error went away.
Solution 7:[7]
Same as @babernethy above, you can have
import 'package:flutter/widgets.dart';
class JosKeys {
static final josKeys1 = const Key('__JosKey1__');
static final josKeys2 = const Key('__JosKey2__');
}
then do this on one of your Global Keys, make sure to have a different JosKeys.josKeys{number} for each global key
GlobalKey _globalKey = JosKeys.josKeys1;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sajjad |
| Solution 2 | Edeson Bizerril |
| Solution 3 | Paresh Mangukiya |
| Solution 4 | Basel Abuhadrous |
| Solution 5 | Mithun S |
| Solution 6 | Waqas ali |
| Solution 7 | Josiah Thobejane |
