Flutter how to use Future return value as if variable

后端 未结 5 1174
离开以前
离开以前 2021-02-18 23:50

I want to get Future return value and use it like variable. I have this Future function

  Future _fetchUserInfo(String id)          


        
5条回答
  •  终归单人心
    2021-02-19 00:43

    You can simplify the code:

    Future _fetchUserInfo(String id) async {
        User fetchedUser;
        var snapshot = await Firestore.instance
            .collection('user')
            .document(id)
            .get();
        return User(snapshot);
      }
    

    you also need async/await to get the value

    void foo() async {
      final user = await _fetchUserInfo(id);
    }
    

提交回复
热议问题