Linked Questions

Popular Questions

I want to test the .toJSON() function of a class because I'm receiving garbage from the gateway, so I need to make sure the type of each fields is okay to avoid a crash.

I created a test by turning the class into a Map<String, dynamic> then using .forEach on it. Something that looks like that :

    map.forEach((String key, dynamic value) {
      switch (value.runtimeType) {
        case int:
          map[key] = rInt[random.nextInt(rInt.length)];
          break;

        case double:
          map[key] = rDouble[random.nextInt(rDouble.length)];
          break;

        case bool:
          map[key] = rBool[random.nextInt(rBool.length)];
          break;

        case String:
          map[key] = rString[random.nextInt(rString.length)];
          break;

        default:
      }
    });

The problem is that since the new instance of the class only contains nulls, the .runtimeType is also null, and I can't generate random values for the class based on the type I have to test.

  • How do I get every key/value pairs of a class and its type in Flutter?
  • How can I invoke a non-specific class member based on a key that's inside a String? (Like its JSON representation)

Related Questions