4

in my main.dart i have among others those two functions:

    Future<void> _fetchMasterData() async {
    print("Start fetch");
    var jwt = await API.attemptLogIn();
    if (jwt != null) {
      Map<String, dynamic> answer = jsonDecode(jwt);
      if (answer['message'] == 'Auth ok') {
        jwtToken = 'Bearer ' + answer['token'];
      }
    }

    await _getArticles();
    await _getMainCategories();
    await _getIngredients();
    await _getArticleIngredients();
    print("EndMasterData fetch");
  }

And

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

What i would like to have is to wait in initState till _fethcMasterData is done bevore Widgert build is called.

Is that possible? Many thanks for any help!

2 Answers 2

1

Here how I use an async func in initstate;

 builder() async {
    favoriteDatabase =
    await $FloorFavoriteDatabase.databaseBuilder('favorite_database.db')
        .build();
    setState(() {
      favoriteDao = favoriteDatabase.favoriteDao;
    });
  }


  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      WidgetsBinding.instance.addPostFrameCallback((_) =>
          getNamePreferences().then(updateName));
    });
    builder();
    favoriteDao.findAllMoviesAsStreamW();
    favoriteDao.findAllMoviesAsStream();
  }

Also you can check this mini article too.

Sign up to request clarification or add additional context in comments.

Comments

0

It is not possible to await in initState, so when you finish all loading process then you can call SetState method which populate your widget with actual data.

Second solution could be use of futurebuilder or streambuilder where you want to show data but it is only possible if any methods data is not dependent on each other.

 Future<void> _fetchMasterData() async {
    print("Start fetch");
    var jwt = await API.attemptLogIn();
    if (jwt != null) {
      Map<String, dynamic> answer = jsonDecode(jwt);
      if (answer['message'] == 'Auth ok') {
        jwtToken = 'Bearer ' + answer['token'];
      }
    }

    await _getArticles();
    await _getMainCategories();
    await _getIngredients();
    await _getArticleIngredients();
    print("EndMasterData fetch");
    SetState((){});  // added line
  } 

7 Comments

I am not sure I understand the question correctly but you can use async with initstate, like, "void initState() async{ _fetchMasterData();}" or ""void initState() async{ await _getArticles();}" etc
Sorry my mistake, but not with this way, "void initState() { _fetchMasterData();}" I'll post an sample from my code as answer then you can check.
as much as i know as async task takes time to complete process meanwhile build method complete building widget, so we don't get ant data from async method. do populate our widget with actual data we must call SetState at the end of async method.
that's correct, but future builder(or stream) is best solution like this case in my opinion. instead of initstate "future: _fetchMasterData()" could work better.
yes i also mention that in my answer but here he is calling many methods one by one, so if he requires first methods calculated data in other one then this approach could be best solution.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.