Skip to content

Unable to cast nested custom object #237

Closed
@manhhavu

Description

@manhhavu

Hi,

It seems that when a class' field is custom object, the SDK is currently unable to cast the correct type to the nested object with an error 'type 'ParseObject' is not a subtype of type '''.

import 'package:parse_server_sdk/parse_server_sdk.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:test_api/test_api.dart';

class TestMother extends ParseObject implements ParseCloneable {
  static const String _keyTableName = 'TestMother';
  static const String _keyChild = 'child';

  TestMother() : super(_keyTableName);

  TestMother.clone() : this();

  @override
  clone(Map map) => TestMother.clone()..fromJson(map);

  TestChild get child => get<TestChild>(_keyChild);
  set child(TestChild value) => set<TestChild>(_keyChild, value);
}

class TestChild extends ParseObject implements ParseCloneable {
  static const String _keyTableName = 'TestChild';
  static const String _keyName = 'name';

  TestChild() : super(_keyTableName);

  TestChild.clone() : this();

  String get name => get<String>(_keyName);
  set name(String name) => set<String>(_keyName, name);

  @override
  clone(Map map) => TestMother.clone()..fromJson(map);
}

void main() {
  const endpoint = '<endpoint>';
  const TIMEOUT = const Timeout(const Duration(minutes: 2));

  setUp(() async {
    // Preferences needed by Parse SDK
    SharedPreferences.setMockInitialValues({});

    await Parse().initialize(
      '<client_id>',
      '$endpoint/parse/',
      autoSendSessionId: true,
      debug: true,
    );
  });

  test('Save and delete object with Parse', () async {
    final child = TestChild()..name = "Baby";

    final mother = TestMother()..child = child;

    await mother.save();

    final response = await TestMother().getObject(mother.objectId);
    final saved = response.result as TestMother;

    // This line throw error "type 'ParseObject' is not a subtype of type 'TestChild'"
    expect(saved.child, isNotNull);
  }, timeout: TIMEOUT);
}

Version: 1.0.22 (same problem with older versions : 1.0.21, 1.0.19)

I don't know if it is expected and is there any workaround for that? I have a lot of nested custom objects to update so using the raw method set(key, value) is very cumbersome.

Thanks for your help,

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions