简体   繁体   中英

How can I run a future before build in flutter?

I am not able to run getMapCurrencies before the build, and I need values in listCurrencies before the view. I can not put the future builder in the build because I dont want to bring listCurrencies many times, just once.

Please help

getMapCurrencies() {
    currencies.getCurrenciesCheck().then((val) {
      listCurrencies = val;
    });
  }

class _CurrencyWidgetState extends State<CurrencyWidget> {
  @override
  void initState() {
    // TODO: implement initState
    getMapCurrencies();
    super.initState();   
  }

What do you mean by "I dont want to bring listCurrencies many times, just once"

FutureBuilder is one way you can do this. It will render the widget once listCurrencies is populated.

Another way is to use a ternary operator

listCurrencies != null ? (widget using listcurrencies) : (a progress indicator)

Edit:

Also you should set listCurrencies using

setState((){
    listCurrencies = val;
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM