Skip to content

JsonObject factory accepts null, but will throw when null is passed #1242

Open

Description

@ahmednfwela

the JsonObject factory accepts a nullable parameter, but if null is passed it will throw

/// Instantiates with [value], which must be a bool, a List, a Map, a num
/// or a String. Otherwise, an [ArgumentError] is thrown.
factory JsonObject(Object? value) {
  if (value is num) {
    return NumJsonObject(value);
  } else if (value is String) {
    return StringJsonObject(value);
  } else if (value is bool) {
    return BoolJsonObject(value);
  } else if (value is List<Object?>) {
    return ListJsonObject(value);
  } else if (value is Map<String, Object?>) {
    return MapJsonObject(value);
  } else if (value is Map) {
    // Allow wrong type map, check individual values.
    return MapJsonObject(value.cast());
  } else {
    throw ArgumentError.value(value, 'value',
        'Must be bool, List<Object?>, Map<String?, Object?>, num or String');
  }
}

Activity

  1. davidmorgan commented on May 22, 2023

    @davidmorgan
    Collaborator

    Thanks--I need to take another look at all the APIs to see if there are breaking changes worth making, I'll consider this one.

  2. self-assigned this
    on May 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions