0

Working on a project thats Stores items to my sqlDb created it following this video by James Montemagno https://www.youtube.com/watch?v=XFP8Np-uRWc&ab_channel=JamesMontemagno my issue now comes when I'm trying to save a list to the sqlDb it shows that it was added however when i retrieve my data my List prop is null.

public class UserTask{
    [PrimaryKey]
    public string ID { get; set; }
    public string Title { get; set; }
    [TextBlob("TagBlobbed")]
    public List<string> TagsList { get; set; }
    public string TagBlobbed { get; set; }
    public string Details { get; set; }
    [Ignore]
    public string Comment { get; set; }

    public UserTask()
    {
        TagsList = new();
    }
 }


public static class PlannerDataService
{
    static SQLiteAsyncConnection db;
    static async Task Init()
    {
        if (db != null) return;
        var databasePath = Path.Combine(FileSystem.AppDataDirectory, "DbTasks.db");
        db = new SQLiteAsyncConnection(databasePath);
        await db.CreateTableAsync<UserTask>();
    }

    public static async Task AddNewTask(UserTask t)
    {
        await Init();
        var task = new UserTask()
        {
            ID = t.ID,
            TagsList = t.TagsList,
            Details = t.Details,
            Title = t.Title
        };
        await db.InsertAsync(task); 
    }
    public static async Task<List<UserTask>> GetUserTasks()
    {
        await Init();
        var tasks = await db.Table<UserTask>().ToListAsync();
        var t = tasks.OrderByDescending(a => a.ID).ToList();
        return t;
    }
    public static async Task RemoveTask(string id)
    {
        await Init();
        await db.DeleteAsync<UserTask>(id);
    }
    public static async Task UpdateTask(UserTask t)
    {
        await Init();
        var task = new UserTask()
        {
            ID = t.ID,
            TagsList = t.TagsList,
            Details = t.Details,
            Title = t.Title
        };
        await db.UpdateAsync(task);
    }
}

I've seen + read questions similar to this and I've tried following their advice to no luck which is why I'm posting for a better solution without changing much of my code.

Pascal Jk
  • 23
  • 4
  • do you have Json.net installed in the project? – Jason Apr 18 '21 at 16:26
  • yes I do but I'm not using it. – Pascal Jk Apr 18 '21 at 16:58
  • from the docs: "A JSON-based serializer is used if no other serializer has been specified using TextBlobOperations.SetTextSerializer method. To use the JSON serializer, a reference to Newtonsoft Json.Net library must be included in the project, also available as a NuGet package." – Jason Apr 18 '21 at 16:59
  • tried the last answer from this link [link](https://stackoverflow.com/questions/14663984/can-i-use-a-list-of-string-in-a-class-intended-for-sqlite/35963602#35963602) but I'm thrown a cannot be null ex – Pascal Jk Apr 18 '21 at 17:05
  • you should check for a null value before attempting to serialize it – Jason Apr 18 '21 at 17:36

0 Answers0