Home > Software design >  Reading external Json file in Dart programm - NO Flutter
Reading external Json file in Dart programm - NO Flutter

Time:01-12

I am new to Dart. it might be a real simple problem, but how is it possible to read a json file in a Dart project. I cant't finde any solution on the internet. I don't wan't to do it in flutter, just with simple Dart. This should be possible, or?

I have to files, one Dart file and a Json file, both in the same directory. The Dart file should now open the Json file and save it to a variable. In python the solution is realy simple, but I found no way in dart.

thanks for helping!

CodePudding user response:

Reading a file and converting to json should be pretty straightforward:

import 'dart:io';
import 'dart:convert';
import 'dart:async';

void main() async {
  final file = File('file.json');
  final content = await file.readAsString();
  final instance = jsonDecode(content);

  // ... profit?
}
  •  Tags:  
  • Related