GetX: lifecycle methods are not called for nested controllers - flutter

I have a configuration of nested GetX controllers, which represent my data model. They look like this (I shortened the code to show only controllers structure):
class AppController extends GetxController {
final package = PackageController().obs;
void openPackage() {
// some code with unzipping, parsing and eventually creating instance of PackageController
package.value = packageController;
}
}
.
class PackageController extends GetxController {
final rounds = RxList<RoundController>();
void addRound() {
rounds.add(RoundController());
}
void deleteRound(int index) {
rounds.removeAt(index);
}
}
.
class RoundController extends GetxController {
final themes = RxList<ThemeController>();
void addTheme() {
themes.add(ThemeController());
}
void deleteTheme(int index) {
themes.removeAt(index);
}
}
It goes deeper, but that's enough for understanding. In my UI widgets I access AppController with final store = Get.put(AppController()); and through it I have access to any nested controller I need.
Now to the problem: lifecycle methods like onInit() are called only for AppController() and not for any of the nested ones. So, is there some trick I need to know, or I use GetX in a wrong way, or what?

The lifecycle methods are called only if the GetX dependency injection system creates the instances. Therefore you need to create these instances with Get.put() or Get.lazyPut() like this:
rounds.add(Get.lazyPut(()=>RoundController()));
themes.add(Get.lazyPut(()=> ThemeController()));
Updated Answer:
Yeah. Get.lazyPut() won't add the controller to the list as it returns void.
So you need to actually use Get.create() for different instances:
Get.create(()=>RoundController());
Get.create(()=> ThemeController());
And then:
rounds.add(Get.find<RoundController>());
themes.add(Get.find<ThemeController>());

Related

How to verify a method inside a method is called in mockito

I was doing some unit testing in flutter with mockito, and I feels unable to verify a method is called within another method. The code I've written so far as follows,
The class I want to test
class A {
void doSomething() {
callMe();
}
void callMe() {}
}
Mocked class
class MockA extends Mock implements A {}
The test I wrote,
test("Test method is called", () {
A a = new MockA();
a.doSomething();
verify(a.callMe()).called(1);
});
When I run the above test I am getting an error
No matching calls. All calls: MockA.doSomething()
(If you called `verify(...).called(0);`, please instead use `verifyNever(...);`.)
If i verify doSomething is called it works, but for a call on callMe within doSomething doesn't work. Is this the default behavior or am I doing something wrong? Please note I need to verify the callMe() method is called when doSomething() is called.
You mocked A and replaced it with MockA. Mocks have no implementation. MockA.doSomething() does nothing and does not and cannot call MockA.callMe().
That A.doSomething() calls A.callMe() should be considered an implementation detail of of doSomething(); making a test rely on that would tightly couple the test to the specific implementation and would be brittle.
You can't use a mock to verify the implementation of the thing being mocked. If you want to verify the implementation of A.doSomething(), you instead should use an actual object and verify observable properties on that object.
But if you still really want to do this, then you would need to modify A to not call methods on itself and to instead call methods on a provided object (i.e., "dependency injection"). For example:
class A {
final late A a;
A({A? a}) {
this.a = a ?? this;
}
void doSomething() {
a.callMe();
}
void callMe() {}
}
test("Test method is called", () {
var mockA = MockA();
var actualA = A(a: mockA);
actualA.doSomething();
verify(mockA.callMe()).called(1);
});
It's a bit unusual for a class to depend on a mock of itself, however, and it would not scale if you then want to verify calls made by callMe().
Another approach that would scale better (but with significantly more work) would be to create your own fake class that tracks method calls:
class TrackedA implements A {
int doSomethingCallCount = 0;
int callMeCallCount = 0;
#override
void doSomething() {
doSomethingCallCount += 1;
super.doSomething();
}
#override
void callMe() {
callMeCallCount += 1;
super.callMe();
}
}
But again, that's very brittle, and I would not recommend it.

Copying objects bug in Flutter BLoC

I am building an app with Flutter using BLOC Architecture with the flutter_bloc package.
I have a data class for an object, which looks like this example:
class MyClass {
int? id;
List<MyOtherClass> myOtherClasses = [];
MyClass();
MyClass._(this.id, this.myOtherClasses);
MyClass copyWith({int? id, List<MyOtherClass>? myOtherClasses}) {
return MyClass._(
id ?? this.id,
myOtherClasses ?? this.myOtherClasses,
);
}
}
class MyOtherClass {
int value;
MyOtherClass(this.value);
}
Now I a Screen that shows the values of the class, and a Dialog to edit it. To store the state, I am using a Cubit, that looks like this:
#immutable
abstract class MyClassState {
final MyClass myClass;
MyClassState(this.myClass);
}
class MyClassInitial extends MyClassState {
MyClassInitial() : super(MyClass());
}
class MyClassEditing extends MyClassState {
final MyClass editingMyClass;
MyClassEditing(MyClass myClass, this.editingMyClass) : super(myClass);
}
class MyClassChanged extends MyClassState {
MyClassChanged(MyClass myClass) : super(myClass);
}
class MyClassCubit extends Cubit<MyClassState> {
MyClassCubit() : super(MyClassInitial());
void editMyClass({int? id, List<MyOtherClass>? myOtherClasses}) {
emit(MyClassEditing(state.myClass,
state.myClass.copyWith(id: id, myOtherClasses: myOtherClasses)));
}
void saveChanges() {
if (state is MyClassEditing)
emit(MyClassChanged((state as MyClassEditing).editingMyClass));
}
void discardChanged() {
emit(MyClassChanged(state.myClass));
}
}
So, basically, what I am trying to achieve here is to story a backup of MyClass in the MyClassEditing state in order to be able to discard the changes I made to MyClass. When I call the constructor of MyClassEditing here in this line: emit(MyClassEditing(state.myClass, state.myClass.copyWith(id: id, myOtherClasses: myOtherClasses)));, the state should contain the initial instance of MyClass without any changes, and the copy of the initial MyClass instance with the changes applied. But somehow, both instances have the changes applied, and I just don't get why this happens. Am I doing something wrong copying the instance?
Probably, the issue might be lists, if you make shallow copy of them.

Flutter Dio Package: How to listen to download progress from another class?

I have a DownloadsService class that handles downloading of file using dio package. I want to listen to the download progress from my ViewModel class that implements the downloadFile method inside my DownloadService class. How do I do this?
Here's my code snippet for DownloadsService class:
class DownloadsService {
final String urlOfFileToDownload = 'http://justadummyurl.com/'; //in my actual app, this is user input
final String filename = 'dummyfile.jpg';
final String dir = 'downloads/$filename'; //i'll have it saved inside internal storage downloads directory
void downloadFile() {
Dio dio = Dio();
dio.download(urlOfFileToDownload, '$dir/$filename', onReceiveProgress(received, total) {
int percentage = ((received / total) * 100).floor(); //this is what I want to listen to from my ViewModel class
});
}
}
and this is my ViewModel class:
class ViewModel {
DownloadsService _dlService = DownloadsService(); //note: I'm using get_it package for my services class to make a singleton instance. I just wrote it this way here for simplicity..
void implementDownload() {
if(Permission.storage.request().isGranted) { //so I can save the file in my internal storage
_dlService.downloadFile();
/*
now this is where I'm stuck.. My ViewModel class is connected to my View - which displays
the progress of my download in a LinearProgressIndicator. I need to listen to the changes in
percentage inside this class.. Note: my View class has no access to DownloadsService class.
*/
}
}
}
The Dio documentation provides an example on how to make the response type into a stream/byte.. But it doesn't give any example on how to do it when downloading a file. Can someone point me to a right direction? I'm really stuck at the moment.. Thank you very much!
I had exactly this issue
I solved issue with dependency injection from my model
actually I defined progress field(double) into my model for listening to download percent and also defined an object from my model into GetX controller(u can use any dependency injection like getIt, ...) so with live data mechanism , this issue will solve so easily
If the View creates the ViewModel, then you must define PublishSubject variable in View class, then pass it to the ViewModel, and also pass it to the DownloadsService as a parameter
like this :
class ViewModel {
PublishSubject publishSubject;
ViewModel(this.publishSubject);
DownloadsService _dlService = DownloadsService();
void implementDownload() {
if(Permission.storage.request().isGranted) {
_dlService.downloadFile(publishSubject);
}
}
}
So that the View listens to the changes that will happen before downloadFile method, Which in turn sends the changes sequentially
like this:
void downloadFile(PublishSubject publishSubject) {
Dio dio = Dio();
dio.download(urlOfFileToDownload, '$dir/$filename',
onReceiveProgress(received,total) {
int percentage = ((received / total) * 100).floor();
publishSubject.add(percentage);
});
}
So that the interface listens to the changes that will happen before
like this:
class View {
PublishSubject publishSubject = PublishSubject();
ViewModel viewModel;
View(){
publishSubject.listen((value) {
// the value is percentage.
//can you refresh view or do anything
});
viewModel = ViewModel(publishSubject);
}
}

Flutter, Dart. Create anonymous class

Maybe it's really dumb question. But I cannot believe there is no resources, where it's described. Even from the official documentation. What I'm trying to do, it's create Anonymous class for the next function.
How to create Anonymous class in Dart with custom function something like next in Kotlin?
Handler(Looper.getMainLooper()).post(Runnable() {
#override
open fun run() {
//...
}
private fun local() {
//....
}
})
Dart does not support creating an anonymous class.
What you're trying to do is not possible.
On the other hand, you can create anonymous functions. So you could use that to mimic an anonymous class.
The idea is to add a constructor of your abstract class, that defer its implementation to callbacks.
abstract class Event {
void run();
}
class _AnonymousEvent implements Event {
_AnonymousEvent({void run()}): _run = run;
final void Function() _run;
#override
void run() => _run();
}
Event createAnonymousEvent() {
return _AnonymousEvent(
run: () => print('run'),
);
}
It's not strictly the same as an anonymous class and is closer to the decorator pattern. But it should cover most use-cases.
This is an alternative way, but not fully equivalent:
Problem, e.g.:
I would like to implement OnChildClickListener inline in my code without class. For this method:
void setOnChildClickListener(OnChildClickListener listener) {
...
}
Instead of this:
abstract class OnChildClickListener {
bool onChildClick(int groupPosition, int childPosition);
}
use this:
typedef OnChildClickListener = Function(int groupPosition, int childPosition);
And in code you can implement it in this way:
listView.setOnChildClickListener((int groupPosition, int childPosition) {
// your code here
});
In other words do not use abstract class, but use typedef.

Print data received by REST call when using #Resource in Grails

Following along with groovies docs on REST, i've setup a model like so:
import grails.rest.*
#Resource(uri='/books')
class Book {
String title
static constraints = {
title blank:false
}
}
I'd print out the parameters I receive when creating and saving. Is there away to override these methods created by the #Resource(uri='/books') annotation? Or handle the annotation a closure or something to do this?
I think you may have 2 choices if you wish to have a default RESTful interface and modify it somewhat for your needs.
Use the $ grails generate-controller [Domain Class Name] command that will generate the appropriate controller and change the generated file as needed.
Create a Book controller and extend the RestfulController; then override the default methods with the #Override annotation, print/log the params, and then call the matching super method.
import grails.rest.RestfulController
class BookController extends RestfulController {
static responseFormats = ['json', 'xml']
BookController() {
super(Book)
}
#Override
def save() {
println params
super.save params
}
#Override
def update() {
println params
super.update params
}
}