Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I am trying to read the XML file in my flutter. But every time I try to read the file, it only throws an error.
This is where I read the xml file(My XML file is large).
convertXMLtoJSON() async {
File file = File('assets/xml_file/belovedskincare.xml');
Future<String> xml = file.readAsString();
And when I run this, it throws this error.
E/flutter (13956): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: FileSystemException: Cannot open file, path = 'assets/xml_file/belovedskincare.xml' (OS Error: No such file or directory, errno = 2)
I have also tried some other functions like
file.readAsLines();
file.readAsStringSync();
But nothing changed.
this is my pubspec.yaml
The assets are working fine. You can see the file structure on the left side in the second image.
I have also tried the rootbundle
but its not working as well.
convertXMLtoJSON() async {
String file = await rootBundle.loadString('assets/xml_file/belovedskincare.xml');
print(file);
when I run this one, it shows this error.
E/flutter (13956): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Null check operator used on a null value
For the AssetBundle, you have to add the WidgetsFlutterBinding.ensureInitialized();
line in the main class.
This is my main class,
void main() {
WidgetsFlutterBinding.ensureInitialized();
FetchData a = FetchData();
a.data();
runApp(const MyApp());
This is where I load my XML file,
class FetchData {
data() async {
print(await rootBundle.loadString("assets/xml_file/belovedskincare.xml"));
If you want to convert the XML file to JSON, you can use the xml2json package.
class FetchData {
data() async {
final xml = await rootBundle.loadString("assets/xml_file/belovedskincare.xml");
Xml2Json xml2json = Xml2Json();
xml2json.parse(xml);
var json = xml2json.toGData();
print(json);
As for the file.readAsString();
, I am still trying to figure out the problem.
convertXMLtoJSON() async {
File file = File('assets/xml_file/belovedskincare.xml');
Future<String> xml = file.readAsString(); // this is where the error happens
You can also use the localhost to read the file(I am using XAMPP). Place the XML file inside the htdocs directory and use the http package to read the file.
For WAMP, place the file inside the www directory.
This is my code,
Replace the localhost in the URL with your IP address. Type ipconfig
in your command prompt to get your IP address(IPv4 Address).
And use the xml2json package to convert the XML file to JSON format.
final jsonData =
await http.get(Uri.parse('http://localhost/file_name'));
final xml = jsonData.body;
Xml2Json xml2json = Xml2Json();
xml2json.parse(xml);
var json = xml2json.toGData();
print(json);
var response = jsonDecode(jsonEncode(json));
print(response);
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.