encode method

String encode (
  1. Object? value,
  2. {Object? toEncodable(
    1. dynamic object
    )}
)
override

Converts value to a JSON string.

If value contains objects that are not directly encodable to a JSON string (a value that is not a number, boolean, string, null, list or a map with string keys), the toEncodable function is used to convert it to an object that must be directly encodable.

If toEncodable is omitted, it defaults to a function that returns the result of calling .toJson() on the unencodable object.

Implementation

String encode(Object? value, {Object? toEncodable(dynamic object)?}) {
  toEncodable ??= _toEncodable;
  if (toEncodable == null) return encoder.convert(value);
  return JsonEncoder(toEncodable).convert(value);
}

© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dart.dev/stable/2.13.0/dart-convert/JsonCodec/encode.html