1

Yes, this is (most likely) a duplicate, but the reason I am making this duplicate is because I have tried the other answers I have found on StackOverflow (and other websites) and none have worked for me. So I assume either I am doing something wrong, or my problem isn't what is being presented to me.

My error is: cannot convert system.threading.tasks.task to system.collections.generic.list

I have read that the problem could be that my method isn't async and that the content I am returning should have await, but unfortunately this didn't do the trick either. I have tried a couple of other solutions, such as making an async method to call from my constructor etc, but unfortunately... No success.

This is what I have right now (do note: I took out the async and await keywords again, since they didn't help):

    public class AButDatabase
    {
    static readonly Lazy<SQLiteAsyncConnection> lazyInitializer = new Lazy<SQLiteAsyncConnection>(() =>
    {
        return new SQLiteAsyncConnection(Constants.DatabasePath, Constants.Flags);
    });

    static SQLiteAsyncConnection Database => lazyInitializer.Value;
    static bool initialized = false;

    public AButDatabase()
    {
        InitializeAsync().SafeFireAndForget(false);
    }

    async Task InitializeAsync()
    {
        if (!initialized)
        {
            if (!Database.TableMappings.Any(m => m.MappedType.Name == typeof(ABut).Name))
            {
                await Database.CreateTablesAsync(CreateFlags.None, typeof(ABut)).ConfigureAwait(false);
            }
            initialized = true;
        }
    }

    public Task<List<ABut>> GetItemsAsync()
    {
        return Database.Table<ABut>().ToListAsync();
    }
}

My second class which I use the database connection in:

       //THE PROPERTIES ARE CORRECT
       public DashboardViewModel()
       {

        if (Connection == null)
        {
            Connection = new AButDatabase();
        }
          //CHANGED THE PROPERTY NAME FOR EASIER READABILITY, IT IS SET HOWEVER
           propNameAButs = Connection.GetItemsAsync(); // causes the error
        }
      }

I have tried a lot and read about the problem itself, the async and await keywords and how they work etc, but unfortunately no success so far.

The only thing that prevents the error, but breaks the application when I try to run it:

propNameAButs = (List<ABut>)(IEnumerable<ABut>)Connection.GetItemsAsync();

The desired behavior what I'm looking for:

To be able to retrieve it as a List and work with it

  • `reason I am making this duplicate is because I have tried the other answers` Please show us your attempt to implement https://stackoverflow.com/a/12520574/34092 . – mjwills Nov 27 '20 at 00:39

1 Answers1

4

you need to use async/await

AButs = await Connection.GetItemsAsync(); 

and

public async Task<List<ABut>> GetItemsAsync()
{
    return await Database.Table<ABut>().ToListAsync();
}

Edit: you mentioned in a deleted comment

I can't, because the constructor isn't async. The error I get then is: The "await" operator can only be used within an async method

to which the answer is,

  1. don't call from the constructor (use OnAppearing)
  2. don't use the SQLite async API
  3. read one of the many existing questions about calling async methods from the constructor
Jason
  • 86,222
  • 15
  • 131
  • 146
  • 1
    a. don't call from the constructor (use OnAppearing), or b. don't use the SQLite async API, or c. read one of the many existing questions about calling async methods from the constructor – Jason Nov 27 '20 at 00:16
  • Copy the OPs comment from my answer, add that and your comment in to the answer, and ill happily upvote to the maximum extent afforded to me by the powers that be – TheGeneral Nov 27 '20 at 00:20
  • @Jason I editted the OP for more clarity. – I try so hard but I cry harder Nov 27 '20 at 00:38
  • That doesn't really change my answer, although using `OnAppearing` only applies in the context of a Page. You haven't explained what specific problem you're having when trying to apply the answers from other similar questions. Another simple fix would be to use an `async Init` method in your VM instead of calling your code from the constructor. – Jason Nov 27 '20 at 02:48