Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter how to use Future return value as if variable

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

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

And I want get value like so

final user = _fetchUserInfo(id);

However when I tried to use like this

new Text(user.userName);

Dart doesn't recognize as User class. It says dynamic.
How Can I get return value and use it?
Am I doing wrong way first of all? Any help is appreciated!

like image 408
Daibaku Avatar asked Aug 29 '18 02:08

Daibaku


People also ask

How do you get the object from an instance of the future in Flutter?

Answer await activerUser(); and if that don't work then try to add the await in the function: return await future; Source: Stackoverflow.com.

How do you find the future value?

You can calculate future value with compound interest using this formula: future value = present value x (1 + interest rate)n. To calculate future value with simple interest, use this formula: future value = present value x [1 + (interest rate x time)].

How do you initialize Future string in Flutter?

One way is to make _future nullable and to make your asynchronous function idempotent by checking if _future is null . If it's null, do work; if it's not null, then just return the existing Future .


3 Answers

You can simplify the code:

Future<User> _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);
}
like image 77
Günter Zöchbauer Avatar answered Oct 14 '22 18:10

Günter Zöchbauer


use like this

var username;
dbHelper.getUsers().then((user) {
  username = user.getName;
});

this function returns future value

dbHelper.getUsers()

it is written like this

getUsers() async { .... }
like image 40
Rashid Iqbal Avatar answered Oct 14 '22 17:10

Rashid Iqbal


To add a little more detail on the original question from Daibaku which might help clarify using Futures in Flutter/dart, user was:

final user = _fetchUserInfo(id);

Without letting compiler infer type, that would be:

final Future<User> user = _fetchUserInfo(id);

Get & Use a Future Value

Daibaku asked how to get the return value of user & use it.

To use user in a Widget, your widget must watch/observe for changes, and rebuild itself when it does.

You can do this manually with a lot of boilerplate code, or you could use FutureBuilder, which has done this for you:

  FutureBuilder(future: user, builder: (context, snapshot) 
  {
    if (snapshot.hasData) return Text(snapshot.data.userName);
    return Container(width: 0.0, height: 0.0,);
  }

More explicitly, you could write the above as:

  FutureBuilder<User>(future: user, builder: (context, AsyncSnapshot<User> userSnapshot)
  {
    if (userSnapshot.hasData) return Text(userSnapshot.data.userName);
    return Container(width: 0.0, height: 0.0,);
  }

FutureBuilder in the above example, synchronously (i.e. immediately) returns a zero width/height Container Widget, but also attaches an observer to your future user, to know when Future data arrives.

When user is eventually updated with "data", FutureBuilder's observer is notified and it calls setState(), which starts a rebuild of itself. Now that snapshot.hasData is true, Text(snapshot.data.userName) is returned instead of the empty Container.


If you weren't using user within the synchronous world of Widget Element tree building and just wanted to print out the value for debugging you could attach an observer using then:

user.then((u) => print(u.userName));
like image 44
Baker Avatar answered Oct 14 '22 18:10

Baker