0

I want to use string value in my code but I am unable to do so. Please help, I am new to flutter.

// Database (db) is database sqlite
// Dog class has a String field name

Text func() async{
     var dog = await db.firstDog();
     return Text(dog.name);
}

The return type Text isn't a Text, as defined by the method func.dart(return_of_invalid_type).

1

3 Answers 3

1

use Future

ForExample

Future<Text> func() async{
    String d = await getTest();
    return Text(d);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Prerequisites

  • Do tell us where this function call is placed (eg. in repository or screen layer)

What you can do

  1. Instead of returning the Text as Widget, you can just return a Future<String>
Future<String> func() async {
     var dog = await db.firstDog();
     return dog.name;
}
  1. Assuming that you are utilizing this on your screen or widget class directly, you can do this
Database db = new Database();
String _dogName;

@override
Widget build(BuildContext context) {
  return Scaffold(
    body: Center(
      child: Text(_dogName),
    ),
  );
}
void func() async {
     setState(() async {
         var dog = await db.firstDog();
         _dogName = dog.name;
     });
}

1 Comment

it is in screen layer
0

Check this article on medium. You can get a good understanding. For asynchronous functions you should use Future class for returning the values.

Future<String> asyncFunc() async {
d = await db.firstDog();
return d.name;
}

Then use this string to set the text for a TextView

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.