How should I tell flutter that my app needs rebuilding externally? - flutter

I want my app to get an intent, and depending on the intent to display something in the app.
Normally, when a widget depends on state, you put it in a State and run setState().
The issue is that when I try starting my flutter app with different intents, I just get I/FlutterActivityDelegate( 4472): onResume setting current activity to this. And in a way it makes sense - I'm not saying anywhere in the flutter code that my widget needs repainting - since I get my intent through Java.
On the other hand, there should be a way to tell flutter to repaint my widget by intent? Or is there something else I should do?

Specifically to get notified about onResume event you can use WidgetsBindingObserver. Implement its didChangeAppLifecycleState method and respond to AppLifecycleState.resumed by doing whatever it is you want the UI to do, such as call setState to trigger rebuild. That said, there needs to be an effective change for the UI to be repainted. Simply calling setState with no effective state change may not result (in fact, should not) in actual UI changes.
In general, you can send a message from Java (or Objective-C/Swift) to Flutter using BasicMessageChannel and make your app react to the message (e.g. call setState or schedule frames).

Related

updateDisplayName in action without reloading simulator

In my code, I have firebase auth and I get the current user as:
User user = FirebaseAuth.instance.currentUser!;
To display the user name in a Text, just use Text(user.displayName)
To test the update functionality, I created a simple button and on onPressed I have
user.updateDisplayName('Test').then((_) =>user.reload());
How to "notify" the Text that the data has changed and change it without needing to reload the simulator?
--> this code is in a drawer
Since you're using Flutter, you'll need to store the value you want to re-render in the State of a stateful widget, call setState() when the value is updated, and then render it from there in your build method.
I recommend checking the documentation on StatefulWidget for an example of this, and more explanation than I can ever repeat here.

Flutter - Listening to one value through whole app

Iam using EventChannel to handle events from hardware barcode scanner. EventChannel is initialized in initState, in main class - it works through whole app. While change is detected, it inserts value into global variable (ValueNotifier - i dont know, if it is right) and then I need to work with that value in multiple widgets. I need some sort of widget, which will tell me, that value updated and it will trigger onEvent function - something like RawKeyboardListener. I tried using listeners, but when i do pushNamed, the listener is still listening and it runs code from previous pages, while scanning.
Is there any widget, that would be suitable for me? (cant use ValueListenableBuilder, because it has no "onEvent" function) Or is there any way, to remove and add listeners while moving between pages, or while modal bottom sheet is opened? (I need to access previous listeners, after Navigator.pop)
I solved my problem by using listeners and ModalRoute.of(context).isCurrent.

How does BlocProvider.value works with added Event

What will happen if An event is added to the bloc being passed inside the BlocProvider.value?
Will the event gets added before or after i get to the page?
And Is Blocprovider.value an ideal way to avoid adding events to bloc in initState() method?
I'm trying to avoid initstate as I'm listening to Stream of data from firebase (this event need to be called only once).
I found a workaround for this,
Using the approach stated above will make the LoadProductFromStream event get called even before getting to /opening the StreamproductPage().
To avoid this a simple and efficient approach could be used instead
create a wrapper widget
create the body (the main widget to display results)
Below is an image attached to explain this
Wrapper widget is where the bloc will be created/initialized
and where LoadProductFromStream() event will be added to the bloc
In conclusion
This way we only add LoadFromStream() event to bloc when we get to StreamproductPage
Takeaway
BlocProvider(create:(context)=> xyzBloc()) // can automatically close bloc
while
BlocProvider.value() // doesn't close bloc automatically
(i stand to be corrected)

Where should my automatic screen change occur - presentation or model?

I am using scopedModel approach in flutter, but the question still remains with other patterns like flutter.
-My UI calls authModel.login()
-authModel.login does async work and calls notifyListeners
-build is automatically triggered on my UI
-inside build method, UI checks the login state on authModel, then calls
addPostFrameCallback to change the screen from the build method.
Is this the best design? I feel like this is code smell, but I am not sure. Would it be better if the authModel was passed an onLoginCompleteCallback function from the UI that would change the screen, and then I would not need to call addPostFrameCallback in the build method?
The presentation on the screen is not dependant on the isLoggedIn bool at all. Its just observed to change the screen.

Trace setState calls in Flutter

I am having an issue in my app with constant rebuilds. I guess there is some side effect that triggers setState.
Is there a way to trace all setState calls in the whole app?
flutter has amazing tools to handle all kind of stuff and specific what you want. Take a look at: https://flutter.dev/docs/development/tools/devtools/overview