flutter wrong order init and dispose methods - flutter

I have question. When we do quik(double click) actions pop()from screen and then push()to this screen. In this case dispose() and init()will be in wrong order. I think dispose shoul call first(pop) and then init(push) when we open screen. But actual result => first init and then dispose.How to fix it in flutter?

I noticed the same unexpected behaviour.
I tried to create a reproduced code https://github.com/antonio-nicolau/Flutter-beamer-with-flutter_hooks
please see logs and you'll notice dipose being called in reverse mode

Related

The backgroundHandler needs to be either a static function or a top level function to be accessible as a Flutter entry point

hey guys when I use onDidReceiveBackgroundNotificationResponse in flutter_local_notification I got an error can someone help me?
here is error
here is my code
actually I want navigate to second page after user select my notification in background mode but I got this problem
put the definition of function used as backgroundHandler outside of any class.
ie for eg:
//if in main.dart
main(){
}
ClassABC{
void getLetter()=>print('a and b');
}
//notice how this is outside of classABC scope and main scope.
backgroundHandler(){
// put handling code here.
}
For more clarity, could you post the whole page code? if not clear.

Flutter detect PointerEvents over Widget

I have a sample code which detects a hovering stylus over a widget.
The code is from this Stackoverflow Quesion.
In short. It binds using GestureBinding.instance?.pointerRouter.addGlobalRoute and checks in the handler if the event is of type stylus.
This gets triggered when the stylus is hovering over the screen (no contact).
It works great on Widgets like Text(), Container() etc.
Question:
I want to use this functionality on a different Widget, the Flutter InAppWebView but the event will not get triggered until the pen has contact with the surface. Even on the Container it does not work, if the child is the InAppWebView.
I think this problem will occur on other Widgets too.
I tried the Listener, AbsorbPointer and IgnorePointer.
Update 1:
I can see the following in the debug output when I start hovering the stylus over the screen.
I/ViewRootImpl(23491): updatePointerIcon pointerType = 20001, calling pid = 23491
D/InputManager(23491): setPointerIconType iconId = 20001, callingPid = 23491
Update 2:
The InAppWebView has an option useHybridComposition which is false by default. Setting it to true solves the issue. But the WebView is becoming very slow.
HERE is a repository that shows the problem.
Thanks!
EDIT
As desribed below, this question has two solutions.
Set useHybridComposition to true. For slowness, maybe raise an issue to that repo.
Hook at android/ios level instead of Flutter level, and forward events back to Flutter.
The debugging method maybe like this: Firstly, print out the pointer events in methods like your _handleEvent. Then you will know whether the pointer event just occur, or they even do not occur.
Secondly, try what widgets are OK and what are not. Text is OK, WebView is not. Then is Container OK? Is InkWell OK? Is IconButton OK? Is IconButton OK? etc. By doing this you gain insight of what is special about Text that makes it work.
Thirdly, as a hacky workaround, could you please try Text.rich(WidgetSpan(child: your_web_view))? Since you say Text is OK while other widgets are not OK.
Lastly, maybe need to dig into Text's source to see what magic happens - probably some special configuration? - to let it work.

Would having multiple compasses in one app work?

I'm new to flutter so I apologize if this seems extremely easy to solve. I'm making an app which uses two compasses. They are put into two separate pages. When I open up the first page, the first compass works just fine. However, when I open up the second page, the second compass moves for a bit then completely stops working. When I try going back to the first page, the first compass stops working as well. I think this is because both compasses uses the same stream but I'm not too sure. The code to make the compasses I took from here: https://pub.dev/packages/flutter_compass/example. Can anyone help?
From what you have stated there can be 2 suggestive solutions from me that could for you:
You are not disposing the stream using the dispose method
#override
void dispose() {
compassController.close();
super.dispose();
}
If disposing the stream also does not work you can use Navigator.pushReplacement instead of Navigator.push which will remove the previous screen because when you push and pop the elements are not disposed(I'm still figuring the reasons behind this)

Provider - Selector rebuild when scrolling

I am using the Selector widget as shown below
Nothing wrong with it's build method, only call it when the value changes.
But when I use Devtools or android studio to track widget rebuild it's showing that the Selector it self rebuild when I am scrolling whether in a list or any other widget that support scrolling.
Yes the Selector didn't call the build method until the value changes but is this normal ?
Using Devtools:
As you can see the others (2) Selectors doesn't have to be triggers but yet they are.
sorry for my bad English, I can explain in another way in the comment section if you didn't understand me and thanks in advance.
edit:
I guess I know why the selector is rebuilding it's self, because I am using the provider class as listener to scroll controller direction with changenotifier.
here the code
in provider class:
bool isHideHomeScreenTabBar = false;
void hideShowTabBar(ScrollDirection scrollDirection) {
isHideHomeScreenTabBar = scrollDirection == ScrollDirection.reverse;
notifyListeners();
}
in my Home screen:
_scrollController.addListener(() {
Provider.of<AppProvider>(context, listen: false).hideShowTabBar(
_scrollController.position.userScrollDirection);
});
So basically the provider trigger changenotifier with every scroll I do and the selector get notified and rebuild it's self but if the value didn't change the selector won't trigger the build method (so it works fine for the child and the widget in the build method of the selector).
But even so is this normal ? and why, The other selectors aren't even listening to the scroll direction.
Anyway I found an alternative way to do this (with an animation controller) but it would be nice if someone could explain what is happening here, it's important for me at least because I might use another state management.
I know what was happing.
I am using 1 class for the provider that contains all the values I need with many methods using notifyListeners, however I thought it's ok to use 1 provider class if I use Selector for every value I had so anything that need rebuild will only rebuild when it's need it.
The problem with this approach is that with every notifyListeners call every selector got notified and rebuild it self (in my case when any scrolling detected) but the selector doesn't call the builder if the value not changed.
The fix is to make some condition that check the old value and the new value before calling notifyListeners, that works prefect in my case, this decrease the rebuilding happing when I scroll to only 1 as it's intended to be, however the other selectors in the same class also rebuild (I guess because they are all in the same class so every notifyListeners call effect them).
At the end if you end up with similar issue it is better to use ProxyProvider or any way that let you use multiple providres, beside the benefit of a better project architecture and disposing it is way better to have more control over the state.
Thanks to RĂ©mi Rousselet Riverpod it's way better than ProxyProvider and I am using it and it's awesome so consider Riverpod if you want to use ProxyProvider.

Show Loading Screen on Home Screen Widget Android

I understand the "android:initialLayout" element within the xml folder for defining the default layout of an Android homescreen widget. I want to be able to display a "loading your information" on my widget while I am waiting for data...how do I do this. I have tried to display an error message on my widget if there is no connectivity, but it doesn't get past the "android:initialLayout" , so showing code I believe is irrelevant. Correct me if I am wrong please...don't bash me. Any help greatly appreciated!
Yes, the initialLayout is so that your widget displays something before Android has had a chance to call your widget-updating code. This is a great place to put a "Loading..." message.
Once your onUpdate method is called in your widget, that is your chance to get your data. If you have a connectivity problem at that point, you can display an error message at that point. Otherwise, if you successfully get your data, then you can draw you widget with the data.
I'm not sure if I've answered your question, so apologies if I have not. Please clarify a bit more if I have not. The main questions I have are:
Is Android calling your onUpdate function? Log statements can be really helpful for this.
Are you trying to load your data in the background, such as a separate Service, and then reading the results in the widget? Or are you loading the data inside the widget?