skip to Main Content

I need to initialize

Future<List<DataRow>> myrow = [];

I need to construct a table based on a future list.

Right now, I get the message:

A value of type ‘List’ can’t be assigned to a variable of type ‘Future<List>’

Thank you

3

Answers


  1. The error occurs because Future<List<DataRow>> expects a future (an asynchronous value) rather than a direct list. To initialize it properly, you need to assign a future or use an asynchronous function to fetch or construct the data

    Login or Signup to reply.
  2. When you got a function that return a Future<List>, this mean two things:

    First, the function is asynchronous and may not return immediately ans second the type of the return value is List.

    So if you have a function prototype like this one:

    Future<List<DataRow>> myFunction() { ... }
    

    you have to get the return value to do this:

    List<DataRow> myrow = [];
    myrow = await myFunction();
    

    The await keyword instruct to wait for myFunction return.

    You can also use .then in order to wait myFunction completion :

    myFunction()
    .then((value) => {
        myrow = value; // In this case value type is List<DataRow>
    });
    
    Login or Signup to reply.
  3. Future<List<DataRow>> myrow = Future.value([]);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search