Skip to content

[firebase_data_connect]: Converting List with Map values throws exception #17348

@mcfrancho1

Description

@mcfrancho1

Is there an existing issue for this?

  • I have searched the existing issues.

Which plugins are affected?

Other

Which platforms are affected?

Android, iOS, macOS, Web

Description

When data is stored as jsonb (AnyValue) in data connect. During parsing of the data when querying, an exception is thrown when toJson() is called on the query result.

NoSuchMethodError: 'toJson'
Dynamic call of null.
Receiver: Instance of '_JsonMap'

After further investigations, it appears the error occurs in the AnyValue class at:

dynamic toJson() { if (value is bool || value is double || value is int || value is String) { return value; } else { if (value is List) { return (value as List).map((e) => e.toJson()).toList(); } else if (value is Map) { // TODO(mtewani): Throw an error if this is the wrong type. return convertMap(value as Map<String, dynamic>); } try { return value.toJson(); } catch (e) { // empty cache to try and encode the value } try { return value; } catch (e) { throw Exception('Could not encode type ${value.runtimeType}'); } } }

When a list contains Map values, this error will always be thrown because dart Maps have no toJson() methods.

Reproducing the issue

Store the following data as jsonb(AnyValue) column to your table(business):
{ "0": { "isOpen": true, "dayIndex": "0", "availabilityHours": [ { "isOpen": true, "endTime": "11:59 PM", "dayIndex": "0", "startTime": "12:00 AM" } ] }, "1": { "isOpen": true, "dayIndex": "1", "availabilityHours": [ { "isOpen": true, "endTime": "11:59 PM", "dayIndex": "1", "startTime": "12:00 AM" } ] }, "2": { "isOpen": true, "dayIndex": "2", "availabilityHours": [ { "isOpen": true, "endTime": "11:59 PM", "dayIndex": "2", "startTime": "12:00 AM" } ] }, "3": { "isOpen": true, "dayIndex": "3", "availabilityHours": [ { "isOpen": true, "endTime": "11:59 PM", "dayIndex": "3", "startTime": "12:00 AM" } ] }, "4": { "isOpen": true, "dayIndex": "4", "availabilityHours": [ { "isOpen": true, "endTime": "11:59 PM", "dayIndex": "4", "startTime": "12:00 AM" } ] }, "5": { "isOpen": true, "dayIndex": "5", "availabilityHours": [ { "isOpen": true, "endTime": "11:59 PM", "dayIndex": "5", "startTime": "12:00 AM" } ] }, "6": { "isOpen": true, "dayIndex": "6", "availabilityHours": [ { "isOpen": true, "endTime": "11:59 PM", "dayIndex": "6", "startTime": "12:00 AM" } ] } }

Store AnyValue to data connect database. Make sure that your

Firebase Core version

3.9.0

Flutter Version

3.29.2

Relevant Log Output

NoSuchMethodError: 'toJson' Dynamic call of null. Receiver: Instance of '_JsonMap' Arguments: []

Flutter dependencies

Expand Flutter dependencies snippet
 firebase_core: ^3.9.0 firebase_crashlytics: ^4.3.4 firebase_data_connect: ^0.1.4+1 firebase_messaging: ^15.1.6 firebase_storage: ^12.1.3 cloud_firestore: ^5.6.0 cloud_functions: ^5.2.0 

Additional context and comments

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions