skip to Main Content

I am using Flutter.

I defined variable like this:

late Future<String?> myvariable;

I get error message saying that myvariable should be initialized.

I cannot assign a value like ‘abc’ to myvariable b/c is future string.

How do I initialize Future string variable?

Thank You

2

Answers


  1. You can try something like this:

    late Future<String> test;
    test = Future.delayed(const Duration(seconds: 0), () => "test");
    
    Login or Signup to reply.
  2. If you have a good default value for myvariable, you can use that with Future.value:

    Future<String?> myvariable = Future.value("my default");
    

    But at that stage, I recommend just getting rid of the Future altogether:

    String myvariable = "my default";
    

    And then use await when you want to reassign a different value to myvariable.


    Also see:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search