Error: get_dataPath can only be called from the main thread

I’m getting this error:

get_dataPath can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.

All I’m trying to do is load a text file from the resources folder of the project. Originally I was just setting the filepath with a string and loading from that but it would not work when publishing to the web.

Code is as follows:

	TextAsset infoFile;
	public SearchField searchfield;
	
	public void Update()
	{
		if (searchfield.Confirm == true)
		{
			
			infoFile = (TextAsset)Resources.Load(searchfield.ToString(), typeof(TextAsset));	
			string[] allcontents = File.ReadAllLines(infoFile.text);
			contents = ConvertStringArrayToStringJoin(allcontents);		
		}
	}

TextAsset infoFile;
public SearchField searchfield;
public void Update(){
if (searchfield.Confirm == true) {
infoFile = (TextAsset)Resources.Load(searchfield.ToString(), typeof(TextAsset));
string contents = infoFile.text;
string allLines = contents.Split(’
');
}
}

Code gives above can read your text file from Resources Folder. if you don’t want array of all lines in seprate field just comment line in which I use Split function.