Skip to content

Not able to change values in registered singleton or user error? #116

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

Closed
enzotar opened this issue Oct 12, 2020 · 12 comments
Closed

Not able to change values in registered singleton or user error? #116

enzotar opened this issue Oct 12, 2020 · 12 comments

Comments

@enzotar
Copy link

enzotar commented Oct 12, 2020

// get it registration in one file
//
sl.registerSingleton<MemoryDatabase>(MemoryDatabase());
sl.registerSingleton<Commands>(Commands());


// in a second file
//
class MemoryDatabase{
   List cache = [];
}

// in a third file, factory singleton with RxCommands
//
class Commands{
   final RxCommand<void, List> doSomething;

   Commands._({this.doSomething});

   factory Commands(){
      List cache = sl<MemoryDatabase>().cache;

      final _doSomething = RxCommand.createSyncNoParam<List>(){
         // some work is done with an output of newCache       

         // this does not work, but I don't get why <<<<<<--------------------------- QUESTION IS HERE
         cache = newCache;
         // and this works
         sl<MemoryDatabase>().cache = newCache;
        
         return [newCache]
      };

      return Commands._(
         doSomething: _doSomething,
      );
   }

}


@escamoteur
Copy link
Collaborator

That's kinda weird. you mean that if you do cache=newCache and you access it later over sl<MemoryDatabase>().cache the data isn't changed?
could you send me a minimal repro project? What you describe should not be possible.

@enzotar
Copy link
Author

enzotar commented Oct 12, 2020

Precisely - the data isn't changed.

Here you go
https://github.com/enzotar/GetItExample.git

Press the floating button multiple times once loaded. Let me know if the example is not clear. Thanks

@escamoteur
Copy link
Collaborator

I was a bit perplexed too here for a moment but it behaves as it should.
The moment you assign ´newCachetocache you replace the content of the variable cache that pointed to the the list inside the MemoryDatabase to a completely different variable.

you can try this here:

class Container{
  List list = [1,2,3];
}


void main() {
  final container = Container();
  
  var cache = container.list;
  
  cache = [5,6,7];
  
 
  print(container.list);
  
}

the surprising thing is , if you don't assign but do an addall on cache it works as you thought.

@enzotar
Copy link
Author

enzotar commented Oct 13, 2020

Shouldn't be the reverse? as in var container.list = cache
Then the container.list is actually changed.

class Container{
  List list = [1,2,3];
}


void main() {
  final container = Container();
  
  
  List cache = [5,6,7];
  container.list = cache;

 
  print(container.list);
  
  
}

@escamoteur
Copy link
Collaborator

that would work, but you asked exactly my code rebuilds

@enzotar
Copy link
Author

enzotar commented Oct 13, 2020

Sorry, I didn't understand your last comment regarding code rebuilds.

Maybe I am confusing stuff, but why would:
container.list = cache work and container.list changes but
sl<Container>().list = cache does't work and container.list is not changed.

@enzotar
Copy link
Author

enzotar commented Oct 13, 2020

I am probably misunderstanding how the code is compiled and the ordering of stuff here.

Do you have any suggestion how one would store a cache? Do I need to set the variable inside a Factory Singleton to always return the same variable? Thanks again

@escamoteur
Copy link
Collaborator

sorry I didn't mean real rebuilds but that my example simulates what you asked.

   factory Commands(){
      List cache = sl<MemoryDatabase>().cache;

      final _doSomething = RxCommand.createSyncNoParam<List>(){
         // some work is done with an output of newCache       

         // this does not work, but I don't get why <<<<<<--------------------------- QUESTION IS HERE
         cache = newCache; // this can't work see my example
         // and this works
         sl<MemoryDatabase>().cache = newCache; /// this here works it's like doing `container.list=cache`
        
         return [newCache]
      };

@enzotar
Copy link
Author

enzotar commented Oct 13, 2020

Looking at it again, I actually think it is not the same as the example. It is the reverse as below. Am I getting it wrong?

class Container{
  List list = [1,2,3];
}


void main() {
  final container = Container();
  
  List cache = [5,6,7];
  container.list = cache;

  print(container.list);
  
  
}

@escamoteur
Copy link
Collaborator

Nope :-)

List cache = sl<MemoryDatabase>().cache; is different to container=container
and you asked why cache = newCache; doesn't work.

@enzotar
Copy link
Author

enzotar commented Oct 13, 2020

Ah I think I'm getting it. This is some Dart trickery! and user error :-) Make me wish for pointers.

@escamoteur
Copy link
Collaborator

yep! I too think pointers would be clearer here. If you use addAll instead of the assignment it works because then cache still holds the instance inside container. But the assignment completely assigns a new instance to cache.
But I too had to think twice and try it :-)

@enzotar enzotar closed this as completed Oct 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants