What is a Future and how do I use it?

后端 未结 2 1702
执笔经年
执笔经年 2020-11-22 04:32

I get the following error:

A value of type \'Future\' can\'t be assigned to a variable of type \'int\'
         


        
2条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 05:11

    Future returning the potential value which will be done by async work

    Eg:

    Future getValue() async {
        return Future.value(5);
      }
    

    Above code is returning Future.value(5) which is of int type, but while receiving the value from method we can't use type Future i.e

    Future value = await getValue();  // Not Allowed
    // Error
    A value of type 'Future' can't be assigned to a variable of type 'int'
    

    To solve above getValue() should be received under int type

      int value = await getValue(); // right way as it returning the potential value. 
    

提交回复
热议问题