Home > database >  Reading external Json file in Dart program - NO Flutter
Reading external Json file in Dart program - NO Flutter

Time:01-13

I am new to Dart. it might be a really simple problem, but how is it possible to read a JSON file in a Dart project. I can't find any solution on the internet. I don't want to do it in a flutter, just with a simple Dart. This should be possible, or?

I have two 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 really 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