Skip to content

type 'Null' is not a subtype of type 'List<dynamic>' in type cast #536

Closed

Description

@alizera

I have a table called city that has id, a1, and a2 columns, I want to do a union all query and get the sum of the particularly same rows, this is my endpoint:

  Future<List<List<dynamic>>> fetchMatchedCity(Session session) async {
    session.log("querying match...");
    final start = DateTime.now().millisecond;

    final result = await session.db.query(""" select id1, id2, sum(score) from (
	    select t1.id as id1 , t2.id as id2, 1 as score from city as t1, city as t2 where t1.a1 = t2.a1 and t1.id <> t2.id union all
        select t1.id as id1 , t2.id as id2, 1 as score from city as t1, city as t2 where t1.a2 = t2.a2 and t1.id <> t2.id
    ) as final
    where id1 > id2
    group by (id1, id2) order by sum(score) desc  """);

    return result;
  }

in main.dart:

....
onPressed: () async {
  await client.city.fetchMatchedCity();
}

I removed the usage of the result of await client.city.fetchMatchedCity(); due to reduce the risk of errors! but I still get following error:

flutter: FormatException: No deserialization found for type dynamic
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: FormatException: No deserialization found for type dynamic
#0      SerializationManager.deserialize (package:serverpod_serialization/src/serialization.dart:73:5)
#1      Protocol.deserialize (package:hlafit_client/src/protocol/protocol.dart:107:18)
#2      Protocol.deserialize.<anonymous closure> (package:hlafit_client/src/protocol/protocol.dart:96:40)
#3      MappedListIterable.elementAt (dart:_internal/iterable.dart:413:31)
#4      ListIterator.moveNext (dart:_internal/iterable.dart:342:26)
#5      new _GrowableList._ofEfficientLengthIterable (dart:core-patch/growable_array.dart:189:27)
#6      new _GrowableList.of (dart:core-patch/growable_array.dart:150:28)
#7      new List.of (dart:core-patch/array_patch.dart:51:28)

P.S: Query is working on db management applications like postico.

Activity

  1. yahu1031 commented on Jan 2, 2023

    @yahu1031
    Contributor

    Similar/duplicates of #426 #432

  2. alizera commented on Jan 3, 2023

    @alizera
    Author

    @yahu1031 I don't understand these issues' points, #432 (comment) didn't work for me, and #426 needs PR I think. So there is no fix for this issue now and I have to wait for the PR, am I right? Thanks, @yahu1031 for answering.

  3. yahu1031 commented on Jan 3, 2023

    @yahu1031
    Contributor

    You can do one thing. Place a debugger there and see what is the data you get and runtime type. As the issue states it is null, we need a deserializer for Null data type too. Check if you have that in serverpod deserialize package. I will update it once I check. @alizera

  4. added
    enhancement
    on Jan 3, 2023
  5. vlidholt commented on Jan 3, 2023

    @vlidholt
    Collaborator

    It's currently not supported to return dynamic objects from Serverpod. You will need to specify a serializable type (the ones you create in the yaml-files). It's possible we can add support for this in the future.

  6. alizera commented on Jan 4, 2023

    @alizera
    Author

    Thanks dear @vlidholt and @yahu1031
    If I get your points and example project correctly:
    1- I created a new module called serverpod_test_shared (actually copied it from the example project) and added it to my server and client dependencies
    2- Add serverpod_flutter: ^0.9.21 into this modules dependencies
    3- Modify the FreezedCustomClass that already exists in this shared module and shape it as I wish to get from my function call:

    @freezed
    class FreezedCustomClass with _$FreezedCustomClass {
      const factory FreezedCustomClass({
        required int id1,
        required int id2,
        required double sum,
      }) = _FreezedCustomClass;
    
      factory FreezedCustomClass.fromJson(
        Map<String, Object?> json,
        SerializationManager serializationManager,
      ) =>
          _$FreezedCustomClassFromJson(json);
    }
    

    My query result on SQL app:
    image

    4- Run flutter pub get and run the build_runner to generate the freezed class of FreezedCustomClass
    5- Modify my endpoint:

     Future<List<FreezedCustomClass>> fetchMatchedCity(Session session) async {
        session.log("querying match...");
        final start = DateTime.now().millisecond;
    
        final result = await session.db.query(""" select id1, id2, sum(score) from (
    	    select t1.id as id1 , t2.id as id2, 1 as score from city as t1, city as t2 where t1.a1 = t2.a1 and t1.id <> t2.id union all
            select t1.id as id1 , t2.id as id2, 1 as score from city as t1, city as t2 where t1.a2 = t2.a2 and t1.id <> t2.id
        ) as final
        where id1 > id2
        group by (id1, id2) order by sum(score) desc  """);
    
        return result as List<FreezedCustomClass>;
      }
    

    6- Call it from my app and now a new error happens:

    flutter: Failed call: city.fetchMatchedCity
    flutter: ServerpodClientException: Internal server error. Call log id: 172, statusCode = 500
    [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: ServerpodClientException: Internal server error. Call log id: 172, statusCode = 500
    #0      ServerpodClient.callServerEndpoint (package:serverpod_client/src/serverpod_client_io.dart:83:9)
    <asynchronous suspension>
    #1      HomePage.build.<anonymous closure> (package:hlafit/pages/home_page.dart:60:19)
    <asynchronous suspension>
    

    My query is working properly on my SQL app. Can you please help me with this? Or fix me if I'm wrong? Thank you guys for your awesome job and support ❤️

  7. yahu1031 commented on Jan 4, 2023

    @yahu1031
    Contributor

    I didn't get exactly that point 2, but to check what the error is, please head over to the logs table in the DB and check for the ID-172 👉🏻 Call log id: 172, statusCode = 500 as it mentioned. @alizera

  8. alizera commented on Jan 4, 2023

    @alizera
    Author

    @yahu1031 Here is the error in serverpod_session_log id 172:

    172	default	2023-01-04 10:46:55.521242		city	fetchMatchedCity	0.03583	0	FALSE	type '_PostgreSQLResult' is not a subtype of type 'List<FreezedCustomClass>' in type cast	#0      CityEndpoint.fetchMatchedCity (package:hlafit_server/src/endpoints/city_endpoint.dart:91:19) <asynchronous suspension> #1      EndpointDispatch.handleUriCall (package:serverpod/src/server/endpoint_dispatch.dart:111:20) <asynchronous suspension> #2      Server._handleRequest (package:serverpod/src/server/server.dart:221:18) <asynchronous suspension> 			2023-01-04 10:46:55.521242
    

    image

  9. yahu1031 commented on Jan 4, 2023

    @yahu1031
    Contributor

    Check with this please,

    type '_PostgreSQLResult' is not a subtype of type 'List<FreezedCustomClass>' in type cast
  10. alizera commented on Jan 4, 2023

    @alizera
    Author

    @yahu1031 I understood the error, but I don't know how to fix it. At the end line of my endpoint method, I cast it to List<FreezedCustomClass> that may cause the error, but before it, it was List<List<dynamic>> that didn't work either.
    Can you please help me to fix it? I think I'm missing something stupidly easy.

  11. yahu1031 commented on Jan 4, 2023

    @yahu1031
    Contributor

    @alizera type casting won't work all the time cause the API endpoint query is not returning the FreezedCustomClass, It returns List<List<dynamic>>. List<List> cannot be typecasted as custom models/Objects. Hope this will clear your doubt.

    I think I'm missing something stupidly easy.

    Not actually cause many will get into issues at this point by directly typecasting the response. (Even I do sometimes 🤣)

    Hope this solves your issue.

  12. alizera commented on Jan 4, 2023

    @alizera
    Author

    Dear @yahu1031, I know what you're saying but I don't know what to do about my issue!!
    Please look at my first comment, I was using List<List<dynamic>> as the return type of the method and I was getting deserialization error!

    I think I can't get the idea behind serialization (I've read its documentation twice and yet... I don't get it).

    Finally, you can see my whole code and database and query and the result of the query (that consists of 2 int and one double columns), can you give me a straightforward solution, please? 🥺

    Thank you again.

  13. yahu1031 commented on Jan 4, 2023

    @yahu1031
    Contributor

    @alizera I can actually help you.

    I was getting a deserialization error!

    IS because it was getting null at that time. But later it was having the result(non-null value) but is not castable. That was the issue you get. Can you post the result here? Or you can contact me on Twitter/LinkedIn. So we can collab.

  14. SandPod commented on Sep 2, 2024

    @SandPod
    Contributor

    We have not revisited this in a while, Is this still an issue or can we close this? There has been som major work regarding serialization and database communication to stabilize the interface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions