Flutter; Show the page once [closed] - flutter

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');
}
}

Related

how to force build when ever a page shows up in flutter

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.

Why not rebuild the flutter widgets every one second instead of using complex state managers?

I've found a very naive and easy approach for state management in flutter. I am storing the state using a hive box (a key value database) and rebuilding the screen at every second with setState.
The widget is not accessing an API. Just the hive database that is very fast and light weigh.
I guess that it is wrong but what is the problem with that approach in practice? Will that use lots of battery, too much CPU, cause crashes, memory leak? I don't think so.
Please, give a good reason not to do it.
late Box db;
main() async {
await Hive.initFlutter();
db = await Hive.openBox('db');
runApp(const MyApp());
}
class _MyAppState extends State<MyApp> {
#override
void initState() {
Timer.periodic(const Duration(seconds: 1), (Timer t) => setState(() {}));
super.initState();
}
#override
Widget build(BuildContext context) {
double someValue = db.get('someValue', defaultValue: 0.0);
// Show someValue in a Text
}
If you are new to flutter the question is seen as very logical and makes sense for the new developer so the sweet and simple answer is yes you can do this. But it's totally against the software development ethics and can make your app much slower than the average flutter app. The main concern during the development of any software especially a mobile app is that you have to think about the lower-end device that does not have good battery life and enough memory to run the most optimized application.
Conclusion
in some cases, you can use this method to periodically rebuild your flutter widget but always remember it will increase the chance of an unexpected crash and definitely will cause some UI lagging issues. In short, the main responsibility of the State manager is not to rebuild the widget tree but to avoid that unnecessary build.
for more insight, you can review your app performance with flutter Dart DevTools

How to await async initialisation code in initState() [duplicate]

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();
}

flutter display message on mount [duplicate]

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");
});
}
;
}

smooth page transition in flutter

I'm working on a flutter application. But the page transition is not smooth. And I can't find the problem to solve it;
The pages stop for a second before finished its transition
I'm using
import 'package:page_transition/page_transition.dart';
Navigator.push(context, PageTransition(type: PageTransitionType.leftToRight, child: Service()));
But even before using this I have this problem
Can Anyone help me, please?
EDIT:
I'm using shared_preferences to save local data like user name and order data and so on.
My initState() usually reads userdata in each page to show it in the drawer
#override
void initState() {
checkInternet();
GetUserData();
GetOrderData();
super.initState();
}