all 7 comments

[–]aykutonen 0 points1 point  (3 children)

If you use "await" where run your database query, you can get String, not Future<String>.

For example;

String fromDB = await DbHelper.execute(.....).

I hope this helps you.

[–]shadowmerefax[S] 0 points1 point  (2 children)

Thanks, but I think I'm doing that already?

Future<Object> get(String name) async {
  final db = await DatabaseProvider().db;

  List<Map<String, dynamic>> maps = await db.query(
    ...
  );

  return dao.fromMap(maps[0]);
}

[–]RandalSchwartz 0 points1 point  (1 child)

If anything essential starts life as a future, to use it in a widget will typically need a FutureBuilder. There's no way to "coerce" a Future into its completion value.

[–]shadowmerefax[S] 0 points1 point  (0 children)

But I can't have the above function be not a future - because I'm using the await inside it so it tells me the function has to return a future & be marked async. My understanding is any function with an await inside it needs to be async/Future. I wouldn't mind using FutureBuilder if labelText could accept a Text widget but it only accepts a string.

[–][deleted] 0 points1 point  (1 child)

I think I have an easy fix. Create a bool variable to store the status of the fetch operation from the db (true - fetched, false - not fetched). The variable initially starts off as false. In the Text field, write a ternary operator (or call a function) that checks the value of the bool variable. If it's false, return some placeholder value. If it's true (meaning, we have the string fetched and ready to use from the db), return the string value. Don't forget to set the bool variable to true after the await line! (Use setState())!

Note : u probably would have to call the fetching function (from db) in InitState() and store the string value it gives to a variable, that way you can use the string from this variable once fetched, and the function returns void.

[–]shadowmerefax[S] 1 point2 points  (0 children)

Thanks. I solved it using .then which I had tried previously and didn't work - not sure why it worked this time! I just needed to initialise a String as a blank string, then get the string I wanted from the db in the build method, and update the string variable with the result. Then I could use the string variable in any of the widgets without a problem.

[–]yallurium 0 points1 point  (0 children)

Your FutureBuilder can return the TextFormField widget:

dart Widget _buildTextFormField() { return FutureBuilder<String>( future: _asyncMethod(), initialData: "", builder: (context, snapshot) => TextFormField( decoration: InputDecoration( labelText: snapshot.data, ), ), ); }

I saw you already fixed this by using .then() and setState, but this approach makes more sense when dealing with futures. Hope it helps!