Skip to content
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

Static Object variable values getting lost / reinitialized #32922

Closed
MarkOSullivan94 opened this issue Apr 18, 2018 · 18 comments
Closed

Static Object variable values getting lost / reinitialized #32922

MarkOSullivan94 opened this issue Apr 18, 2018 · 18 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language).

Comments

@MarkOSullivan94
Copy link

Dart SDK version: 2.0.0-dev.47.0.flutter-f76dad0adc
OS: Tested on both Linux (Elementary OS 0.4) and Mac OS High Sierra 10.13.3

Currently the static keyword doesn't seem to be working as it should whenever you have a List of objects. Whenever I check the length of the static list in another class which wasn't the same class where I added an item to the list, the length returns 0.

Example:

I have four dart classes listed below:

  • item.dart -> contains details about the item.
  • item_list.dart -> contains a static List
  • cart.dart -> checks List
  • shop.dart -> allows for adding / removing to List

Whenever I add in shop.dart multiple items to my static list and then check the same static list in cart.dart, it returns nothing in the list.

I have checked Stack Overflow and I've found people with similar issues: https://stackoverflow.com/questions/45772318/flutter-dart-static-variables-lost-keep-getting-reinitialized

@kevmoo kevmoo added the area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). label Apr 18, 2018
@zoechi
Copy link
Contributor

zoechi commented Apr 19, 2018

That's a known issue when you have relative imports in lib/main.dart (imports that don't start with import 'package:...';

@MarkOSullivan94
Copy link
Author

@zoechi so it works fine if you have import 'package:....';?

@zoechi
Copy link
Contributor

zoechi commented Apr 19, 2018

Ok, then this was the issue.

@MarkOSullivan94
Copy link
Author

MarkOSullivan94 commented Apr 19, 2018

I wasn't saying it was working fine, I'm asking you if it works fine when you do that import? It sounded like you were saying in your first post that this is why the static variables weren't working.

@zoechi
Copy link
Contributor

zoechi commented Apr 19, 2018

It was a guess. It's a common issue.
You should only have import 'package:...'; kind of imports in lib/main.dart.

@MarkOSullivan94
Copy link
Author

I never knew that. Definitely should have some kind of warning for this in Flutter projects.

Still I think this issue still stands as it's unexpected behavior.

@kostaa
Copy link

kostaa commented May 11, 2018

Well import with or without the package keyword - it doesn't work either way for me.

@danielhintz
Copy link

For me it works only if for local files I leave out the package:, so all of my local files I just do import ... leaving out package: and it works but it definitely seems like a bug

@zoechi
Copy link
Contributor

zoechi commented Jun 14, 2018

@kosta @danchintz might be a different problem. I'd suggest creating a new issue with a reproduction project, Dart version, ... if possible.

@kostaa
Copy link

kostaa commented Jun 14, 2018

@zoechi @kevmoo

I have put together two simple self-explanatory app to demonstrate the issue.

(1) MyApp class contains a static property called staticProperty that is first set to 'Initial Value' and then changed in the MyApp::build to 'Changed Value' and printed out.

class MyApp extends StatelessWidget {

  static String staticProperty = 'Initial Value';

  @override
  Widget build(BuildContext context) {

    print('(1) MyApp::build [${MyApp.staticProperty}]');
    staticProperty = 'Changed Value';
    print('(2) MyApp::build [${MyApp.staticProperty}]');

Which prints out:
I/flutter ( 5285): (1) MyApp::build [Initial Value]
I/flutter ( 5285): (2) MyApp::build [Changed Value]

So far so good.

Then in the MyHomePage::build I call

  ///
  @override
  Widget build(BuildContext context) {

    print('(3) MyHomePage::build [${MyApp.staticProperty}]');

Which prints out a re-initialized value:
I/flutter ( 5285): (3) MyHomePage::build [Initial Value]

This the version I am running:
Channel master, v0.5.2-pre.58, on Microsoft Windows [Version 10.0.17134.112]

flutter_static_issue.zip

(2) Here is a modified version of the app that uses a global variable instead of the static property. The issue is the same:

I/flutter ( 5778): (1) MyApp::build [Initial Value]
I/flutter ( 5778): (2) MyApp::build [Changed Value]
I/flutter ( 5778): (3) MyHomePage::build [Initial Value]

flutter_static_issue_2.zip

@zoechi
Copy link
Contributor

zoechi commented Jun 15, 2018

@kostaa
I think it's the same issue.
Just don't import lib/main.dart anywhere.

@zoechi
Copy link
Contributor

zoechi commented Jun 15, 2018

@lrhn I think this can be closed as dup of #33076

@lrhn
Copy link
Member

lrhn commented Jun 15, 2018

Agree, it's a recurring problem. It was always a problem, but it's intensified since Flutter put the main file into lib (usually pub packages had a main file in bin or web, which should then not import things in lib using a relative path, but it's much easier to use relative paths when you are right next to the file).

@zoechi
Copy link
Contributor

zoechi commented Jun 15, 2018

@lrhn @kostaa imports package:my_package/main.dart from package:mypackage/my_page.dart,
(no relative imports, but imports entry-point file from another file)
but that also seems to cause these troubles.

@matanlurey
Copy link
Contributor

Duplicate of #33076.

@KLD
Copy link

KLD commented Jun 29, 2022

My issue was I renamed a file from 'Storage' to 'storage'. But in main, it was imported 'Storage' so other files which were using import 'storage' couldn't see my static variable.

@sebthom
Copy link

sebthom commented Aug 6, 2024

@KLD just encountered the exact same issue in Dart 3.4.4. Thanks for the time saver. I had a dart file called settings.dart, imported it in a few places and later refactored and renamed it to Settings.dart but forgot to change the import statement accordingly in one place. I think it is a bug that the Dart compiler does not complain about this and instead silently instantiates the module twice.

@lrhn
Copy link
Member

lrhn commented Aug 7, 2024

It's not a bug (it's working as intended), but it is an annoyance.

The problem is that the language, and the compiler even, cannot really determine if the file system is case sensitive.
Windows can have non-case-sensitive directories, and MacOS can have case-sensitive ones, the operating system is at most a hint.

(What the language could potentially do is to disallow having multiple files that differ only in casing. It has to be casing of the entire path, because file:///foo/Bar/baz.dart and file:///foo/bar/baz.dart may or may not be different files depending on whether the bar directory is case sensitive or not, and that directory may be outside of the Dart package, so any Dart recommendation about naming diretories won't really matter. And then we'll have to decide which case-folding to use, which may or may not be the same as the one used by the different file systems.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language).
Projects
None yet
Development

No branches or pull requests

9 participants