getJsonDataById method

dynamic getJsonDataById(
  1. String? id
)

get jsonData from document that has id named id if not throw Exception. Automatically decodes Nuxt.js NUXT_DATA format if detected.

Implementation

getJsonDataById(String? id) {
  if (id?.isEmpty ?? true) return jsonData;
  final data = document?.getElementById(id!)?.innerHtml;
  if (data == null) {
    throw Exception("invalid json id: $id");
  }
  final decoded = jsonDecode(data);

  // Check if this is Nuxt.js __NUXT_DATA__ format and decode it
  final nuxtDecoded = decodeNuxtData(decoded);
  return nuxtDecoded ?? decoded;
}