flutter_json_to_dart 1.0.0 copy "flutter_json_to_dart: ^1.0.0" to clipboard
flutter_json_to_dart: ^1.0.0 copied to clipboard

Package to compile json to Dart code

Json2Dart #

Json2Dart is a package to converts your map (json) to a String, wich is the output Dart code. You can write the String to a file or put it in a text widget.

How to use #

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

import 'package:json2dart/json2dart.dart';

void main(List<String> args) {
  // Your json String
  String jsonString = '''
{
    "weather": "sunny",
    "clouds": "no",
    "time": "11.30",
    "sports you can do": [
        "running",
        "hiking",
        "biking"
    ],
    "any map": {
        "key 1": "value 1",
        "key 2": "value 2"
    }
}''';

  // Decode the json String to a Map
  Map<String, dynamic> jsonData = jsonDecode(jsonString);

  // Compile the json to Dart code
  final String dartCode = Json2Dart.compileJson2Dart(jsonData, null);

  // Output
  print("Json:");
  print(jsonData);
  print("");
  print("Dart:");
  print(dartCode);

  // Or write to a file
  File dartCodeFile = File("${Directory.current.path}/example/generated_json.dart");
  dartCodeFile.writeAsString(dartCode);
}
0
likes
140
pub points
47%
popularity

Publisher

unverified uploader

Package to compile json to Dart code

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

More

Packages that depend on flutter_json_to_dart