insert method

  1. @override
Future<bool> insert(
  1. String? jsonOrXml,
  2. int? index, {
  3. bool notifyListeners = true,
})
override

Implementation

@override
Future<bool> insert(String? jsonOrXml, int? index,
    {bool notifyListeners = true}) async {
  // make a copy of the first item if no
  // jsonOrXml is specified
  if (isNullOrEmpty(jsonOrXml)) {
    jsonOrXml = (data?.isNotEmpty ?? true)
        ? Json.encode(Json.copy(data![0], withValues: false))
        : null;
  }
  if (isNullOrEmpty(jsonOrXml)) return true;

  // set the index
  index ??= data?.length ?? 0;
  if (index.isNegative) index = 0;

  // add to existing data set
  data ??= Data();
  for (var item in Data.from(jsonOrXml)) {
    if (index! < data!.length) {
      data!.insert(index, item);
    } else {
      data!.add(item);
    }
    index++;
  }

  // template the first record
  template ??= (data?.isEmpty ?? true)
      ? null
      : Json.encode(Json.copy(data![0], withValues: false));

  // notify listeners of data change
  if (notifyListeners) notify();
  onDataChange();

  return true;
}