This question already has answers here:
Flutter: Run method on Widget build complete
(14 answers)
Closed 3 years ago.
Beginner question:
I'm trying to display a message as soon as the page display, I can do it with a simple custom message, but I couldn't find any standard way to do it, all search I did leads me to Snackbar or Flushbar, the problem with this 2 is that u can only show it after the build is called, e.g. on onPress, which leads me to the actual question:
How do I get a call back for onMount event, e.g. something like jQuery ready method, that is triggered once the page is mounted (i.e. ready), so that I can call Snackbar for example?
Duplicate to Flutter: Run method on Widget build complete
answer:
#override
void initState() {
super.initState();
if (SchedulerBinding.instance.schedulerPhase ==
SchedulerPhase.persistentCallbacks) {
SchedulerBinding.instance.addPostFrameCallback((_) {
showInFlushbar("Some text");
});
}
;
}
Related
Hello hope you guys are ok.
the problem I'm facing is we have a main_page which leads to a page doing some changes on data which are show on the main page.
after some process if the user touches back button and goes to main_page app loads it from stack and the data are not shown because it does not get rebuilt.
I don't want to control back button because there are other pages which lead to data changing page and I also tried using valuelistenablebuilder but I don't know why it goes wild and gets into a screen refresh loop without even changing the valueListenable I used redux to manage the value.
actual reason I want main page to rebuild is to call a method. why do I not call that method in second page is complicated and because i don't want to.
in conclusion I want main page to rebuild whenever it shows up even when it's read from the stack or even is there a way to tamper with stack of the pages without visual changes to the user.
you need to use Shared preferences plugin
https://pub.dev/packages/shared_preferences
If I understood the question correctly
The flow should be:
Screen A
#override
void initState() {
loadSomeDataFromDB();
super.initState();
}
Screen B
#override
void initState() { *//INIT AS EXAMPLE*
changeSomeDataFromDB();
super.initState();
}
You can try this in Screen A
onPressed: () async {
bool? shouldLoadDataAgain = await
Navigator.of(context).push(PageRouteBuilder(
pageBuilder: (context, animation, secondaryAnimation) =>
const ScreenB(),
));
if(shouldLoadDataAgain!=null&&shouldLoadDataAgain ==true){
loadSomeDataFromDB();
}
},
and this in Screen B when user press back button
onPressed: () async {
Navigator.of(context).pop(true)
},
solution for my problem was that used redux store had an object and updating object property or a list does not count as variable change to redux so the widget wouldn't rebuild.
This question already has answers here:
What is a Future and how do I use it?
(6 answers)
Closed 1 year ago.
I have some code I need to run in my initState() method as it initialises a late variable widgets in the build method depend on. The problem is the method I want to run is asynchronous as I am loading data from the disk using the Shared Preferences package. I tried making the method asynchronous but I get an error in my IDE saying that the initState() method can't be asynchronous. I tried looking up solutions and they said that you can run them without awaiting in the initState() method. I tried this but the problem is that it doesn't await the initialisation so I get the red error screen saying the variable hasn't been initialised. I originally was going to run this in a constructor of a custom class I had that deals with the data but I also get an error saying that the constructor method can' t be async.
My IDE suggested using the didChangeDependencies() as it can be async, however, the build method doesn't await it.
I tried all the answers in this question (Is there a way to load async data on InitState method?) that didn't suggest a StreamBuilder as I am not trying to load a stream that has to do with Authentication. I also tried to change around the order calling super.initState(); before and after the code, etc. and it didn't work.
make a function that is async and perform your asynchronous work in it ..
then call it in initState without async await keywords but like this
#override
void initState() {
checkLoggedIn().then((user) {
// do anything with your data here
});
super.initState();
}
I am struggling with the following problem:
I built a list of widgets using StreamBuilder (and made it searchable). The widgets are Cards and inside them the user can make his selection and then push a button.
Everything is (was) working just fine.
Then I wanted to add a little animation and make the Icon associated to the button an animated one.
Now it is a mess, the StreamBuilder is in an infinite loop and I have also some problem on my list. If I comment out the animated icon and put in again the previous Icon ...everything starts to work fine again.
...
child: FloatingActionButton(
onPressed: (){
addFood();
mealListState.getAcontroller(id).forward();
},
child:
// MyAddIcon(id), //--> my animated Icon. It does not work
Icon(Icons.add), //--> it works
....
I read that the problem is that adding States management (Unnecessary Widget Rebuilds While Using Selector (Provider) inside StreamBuilder) inside the stream mess things up and that you have to make the widget building the stream Stateful and set up the stream in the initState.
I tried to follow this way but I need the context to build my card list, so I could follow the above hint just to read the data from db (firestore) and ...it is not enough
Could someone point me in the right direction or I have to leave the idea and move on?
Thanks
If you need the context, you can try to listen to the the stream in the didChangeDependencies method of your State class.
Use the Streamsubscription returned by stream.listen to cancel your subscription like so to avoid subsribing twice or even more times:
#override
didChangeDependencies() {
final stream = Provider.of<MyStream>(context); // context available here.
if (this.streamsubscription != null) {
this.streamsubscription.cancel();
}
this.streamsubscription = stream.listen((value) {
// your callback code here
});
super.didChangeDependencies(); // important
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Hi I want the user make a choice when the application is first opened. After that, I don't want user to see this page every time enters the application. (If user wants, user can switch to the page from the drawer menu.)
What should i do to do this?(I already have a splash screen in my application. This is a separate page)
We can utilize SharedPreferences package.
After user visits the BaseScreen page, we have to write 'log', 'note', or 'tracenote', locally in the device.
There are already package options, e.g, SQFlite, Hive, but personally I recommend Shared Preferences package.
Below I showed you the basic code so you can get the idea.
Code
class BaseScreen extends StatefulWidget {
#override
_BaseScreenState createState() => _BaseScreenState();
}
class _BaseScreenState extends State<BaseScreen> {
#override
void initState() {
super.initState();
navigateToRealAppScreen();
}
void navigateToLastPage() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
String isOpened = prefs.getString('first_screen_already_opened');
// Need to push to another screen, if already opened
if (lastRoute == 'true') {
Navigator.of(context).pushNamed('/realAppScreen');
}
}
This question already has answers here:
Flutter: Run method on Widget build complete
(14 answers)
Closed 3 years ago.
The problem is, I need to call method after widget is built, otherwise I'm getting an error.
So I want to know is there a way to check whether widget is built, maybe a listener?
All widgets have a bool this.mounted property. It turns true when the buildContext is assigned.
Tip: Its a good practice to set the state of any widget only after the widget is built and I usually use this bool to set states once it is true. It is an error to call setState when a widget is unmounted or before it is mounted.
In your case, I feel you need the same thing, run your method if this.mounted == true.