Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter, function needs to wait till data is available

I am a newbie on flutter programming. I am trying to understand async function calls, but got off track.

I want to store data on my device in a secured way. So, when writing the data, the data will be encrypted and when reading the data, the data will be decrypted. The application is not aware that the data is encrypted on the device.
I have made a Storage class which makes use of the SharedPreferences library.
A second functionality of the reading function, is that the application will wait till the data is available.

I wrote something like this

  static String getString(String key)  {
    SharedPreferences prefs = await SharedPreferences.getInstance();

    return _decrypt(prefs.getString(key));   
  }

Now the compiler complains that the await statement is not expected.

When I change the function to an async function returning a Future, it works. However my problem of waiting moves up, to the place where the getString function is called.
Goal: the function shall return the decrypted String and the main thread needs to wait till the data is available.

Added from an answer by the OP that should have been an edit:

Sorry not to be specific enough. My page is loading in it's own thread. Within the thread I want to get the value from the store. The thread cannot continue since the next statement depends on the data from the store.

E.g. I need username and password from the store to be able to login. username and password are stored separately.

Of course I can nest 'then' statements, but I think for readability of the source code it is better to wait till the value is read from the store.

like image 816
user3412673 Avatar asked Aug 08 '26 14:08

user3412673


1 Answers

You should never block the main thread, because it blocks app operations. Your intialization might take long time and system can kill your apps if necessary.

To solve your problem, it is better not to block main thread, and it can be done by creating a loading screen, wait for data loading to complete, and show the final app screen.

The code prototype could be like following:

class HomePage extends StatefulWidget {
  @override
  HomePageState createState() => HomePageState();
}

class HomePageState extends State<HomePage> {
  String data;

  @override
  void initState() {
    super.initState();

    loadData().then((data) {
      setState(() {
        this.data = data; 
      });
    });
  }

  Future loadData() async {    
    data = 'xxx'; // load your data from SharedPreferences
    return data;    
  }

  @override
  Widget build(BuildContext context) {
    if (data == null) {
      loadData();
      return LoadingScreen();
    } else {
      return MainScreen();
    }
  }
}
like image 106
Pete Houston Avatar answered Aug 11 '26 06:08

Pete Houston



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!