Flutter, function needs to wait till data is available

12,592

Solution 1

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

Solution 2

You have to add the keywords async and Future as follows:

static Future<String> getString(String key)  async { // ------> here
   SharedPreferences prefs = await SharedPreferences.getInstance();

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

and to consume this asynchronous method you must do it in the following way:

getString("someKey").then((valueString) => print(valueString));
Share:
12,592
user3412673
Author by

user3412673

Updated on June 05, 2022

Comments

  • user3412673
    user3412673 almost 2 years

    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.

  • greybeard
    greybeard about 4 years
    What question does this answer, and how? Please heed How do I write a Good Answer?