George Harrison
George Harrison

Reputation: 71

Blazor list of data erroring on return

Im currently trying to make a list of data to display in a for loop on my blazor website. Everything else on the page is written right i just cannot get the return right for the service method.

public Task<List<FirstAider>> GetFirstAidersAsync()
        {
            List<FirstAider> f = new List<FirstAider>
            {
                new FirstAider { Name = "First Aider 1", EmailAddress = "[email protected]", Telephone = "01101000 01101001 00001101 00001010" },
                new FirstAider { Name = "First Aider 2", EmailAddress = "[email protected]", Telephone = "01101000 01101001 00001101 00001010" },
                new FirstAider { Name = "First Aider 3", EmailAddress = "[email protected]", Telephone = "01101000 01101001 00001101 00001010" }
            };

            return f.ToArray();
        }

I have attached the issue below but i have had to scribble out work project file names unfortunately so sorry for the inconvenience.

It's erroring on the f.ToArray();

VS Error Picture

Upvotes: 1

Views: 355

Answers (1)

Michael Hancock
Michael Hancock

Reputation: 2855

Your method has a return type of Task<List<FirstAider>> but you are returing an array Array<FirstAider>. The method does not need to return a task or be async since it does not await any async operations. Change the return type to Array<FirstAider> to fix the error.

Alternatively if you need the method to return a task you could do the following

public Task<List<FirstAider>> GetFirstAidersAsync()
{
    List<FirstAider> f = new List<FirstAider>
    {
        new FirstAider { Name = "First Aider 1", EmailAddress = "[email protected]", Telephone = "01101000 01101001 00001101 00001010" },
        new FirstAider { Name = "First Aider 2", EmailAddress = "[email protected]", Telephone = "01101000 01101001 00001101 00001010" },
        new FirstAider { Name = "First Aider 3", EmailAddress = "[email protected]", Telephone = "01101000 01101001 00001101 00001010" }
    };

    return Task.FromResult(f);
}

More information about Task.FromResult here

Upvotes: 3

Related Questions