Want to load file for testing in Flutter? I found a crude work around for the meantime. Basically, I have a function which tries both ways to specify the file path.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import 'dart:convert'; import 'dart:io'; Future<Map<String, dynamic>> parseRawSample(String filePath, [bool relative = true]) async { filePath = relative ? "test_data/src/samples/raw/$filePath" : filePath; String jsonString; try { jsonString = await File(filePath).readAsString(); } catch (e) { jsonString = await File("../" + filePath).readAsString(); } return json.decode(jsonString); } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.