-
Notifications
You must be signed in to change notification settings - Fork 26.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GlobalKey not attaching to a widget #91557
Comments
Looks like the correct behavior to me. To access the context of a Widget with a |
@lukaszdebowski import 'package:flutter/material.dart';
final keys = [for (var i = 0; i < 5; ++i) GlobalObjectKey(i)];
void main() {
runApp(
MaterialApp(
home: Scaffold(
body: ListView(
children: [for (final key in keys) FlutterLogo(key: key)],
),
floatingActionButton: FloatingActionButton(
onPressed: () => print(keys.first.currentContext != null),
),
),
),
);
}
@Vardiak |
According to GlobalObjectKey documentation and what I've tested, it works like that.
|
@iapicca The code from your answer works correctly, which is even more confusing to me |
worse case you spotted an inconsistent behavior, which can still be valuable |
@iapicca The following also works, so I'm not really sure what is going on here
|
@iapicca Okay I've spotted the issue. It has something to do with string interpolation and how the
|
Here is the solution for you: https://pub.dev/packages/assorted_layout_widgets Then use the provided |
@marcglasberg Thank you. Although I do have a solution, I still think that this is an issue in the language - it should either return true for both cases (dartpad and local) or return false. It is also not clear why creating the strings from interpolation and as a string value ('abc1') gives different results from the 'identical' function (if it compares the objects in memory, shouldn't it return false in both cases as the strings are not created as 'const'?) |
@marcglasberg |
@lukaszdebowski It's NOT an issue in the language. The @iapicca No, it is NOT a workaround. The |
@lukaszdebowski Sorry, I forgot to say, also, that Strings are automatically |
Oh I didn't know that, that would explain the identical behavior. But if that's the case, them the dartpad example should work the same way, which it doesn't - so if you are correct, that means the web part is implemented wrong (the identical implementation). I'm fine with either result, it's just that it should be consistent across platforms |
Problem is dartpad is Dart compiled to Javascript, and Javascript doesn't make the distinction. Some details of Dart are not the same in all platforms. The language specification allows for that (not being consistent in all platforms). |
I've found other comment that confirms the behavior: https://stackoverflow.com/a/16261723 So I guess it's solved, even tho it kinda doesn't feel right about the identical on the dartpad 😄 |
Based on the conversation thus far and #91557 (comment) I feel it is safe to close this issue. As such, I will be closing this issue. Thank you |
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 |
Hi!
I am trying to understand the problem of not attaching a GlobalKey to a widget.
The problem occurs only when the widgets are built using a for loop like in the example.
Building normal widgets (without for loop) correctly attaches the GlobalKey and the context can be found without problems.
Steps to Reproduce
container0: true
printed in the consolecontainer0: false
printed insteadExpected results: see
container0: true
printed in both casesActual results: running the exact same code on local IDE in the simulator gives
false
Logs
flutter doctor -v output
Edit
After some further investigation I've found that the problem lays in the way GlobalObjectKeys are created.
It has something to do with string interpolation:
Creating the keys as strings with int values such as
GlobalObjectKey('abc1')
cannot find the context, whileGlobalObjectKey(1)
can.The
identical
function seems to have some problem -> #91557 (comment)Still don't know why the same code has different behaviour in the dartpad and locally
The text was updated successfully, but these errors were encountered: