logger
logger copied to clipboard
Fails to encode map.keys
Converting object to an encodable object failed: Instance of '_CompactIterable<String>'
print() had handled this without error.
When did you run into this exception? Can you tell me which printers you were using? Also please provide the entire stacktrace/error output.
I've ran into the same issue. Here's what was outputted in vscode:
E/flutter ( 7377): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Converting object to an encodable object failed: Instance of '_CompactIterable<String>'
E/flutter ( 7377): #0 _JsonStringifier.writeObject (dart:convert/json.dart:659:7)
E/flutter ( 7377): #1 _JsonStringStringifier.printOn (dart:convert/json.dart:846:17)
E/flutter ( 7377): #2 _JsonStringStringifier.stringify (dart:convert/json.dart:831:5)
E/flutter ( 7377): #3 JsonEncoder.convert (dart:convert/json.dart:260:30)
E/flutter ( 7377): #4 PrettyPrinter.stringifyMessage (package:logger/src/printers/pretty_printer.dart:184:22)
E/flutter ( 7377): #5 PrettyPrinter.log (package:logger/src/printers/pretty_printer.dart:94:22)
E/flutter ( 7377): #6 Logger.log (package:logger/src/logger.dart:115:29)
E/flutter ( 7377): #7 Logger.d (package:logger/src/logger.dart:80:5)
E/flutter ( 7377): #8 Debug.log
package:grovemobile/Logger.dart:20
E/flutter ( 7377): #9 new ChatMessage
package:grovemobile/models/chatMessage.dart:50
E/flutter ( 7377): #10 ChatMessage.create
package:grovemobile/models/chatMessage.dart:116
E/flutter ( 7377):
Ah. That makes sense. Seems you are passing a ChatMessage as a value of a map key to the printer? Then that ChatMessage needs to implement a String toJson() method.
Ah. That makes sense. Seems you are passing a
ChatMessageas a value of a map key to the printer? Then thatChatMessageneeds to implement aString toJson()method.
How ablout if Class don't have toString method, print Instance of 'xxxx' instead of throw Error.
Package version: 1.0.0-nullsafety.0
I encountered this same error today with the Camera plugin passing in an array of CameraDescription objects
E/flutter (23935): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: Converting object to an encodable object failed: Instance of 'CameraDescription'
E/flutter (23935): #0 _JsonStringifier.writeObject (dart:convert/json.dart:688:7)
E/flutter (23935): #1 _JsonPrettyPrintMixin.writeList (dart:convert/json.dart:793:7)
E/flutter (23935): #2 _JsonStringifier.writeJsonValue (dart:convert/json.dart:718:7)
E/flutter (23935): #3 _JsonStringifier.writeObject (dart:convert/json.dart:679:9)
E/flutter (23935): #4 _JsonStringStringifier.printOn (dart:convert/json.dart:877:17)
E/flutter (23935): #5 _JsonStringStringifier.stringify (dart:convert/json.dart:862:5)
E/flutter (23935): #6 JsonEncoder.convert (dart:convert/json.dart:262:30)
E/flutter (23935): #7 PrettyPrinter.stringifyMessage
package:logger/…/printers/pretty_printer.dart:201
E/flutter (23935): #8 PrettyPrinter.log
package:logger/…/printers/pretty_printer.dart:101
E/flutter (23935): #9 Logger.log
package:logger/src/logger.dart:115
E/flutter (23935): #10 Logger.i
package:logger/src/logger.dart:85
print was able to print it just fine:
I/flutter (23935): [CameraDescription(0, CameraLensDirection.back, 90), CameraDescription(1, CameraLensDirection.front, 270), CameraDescription(2, CameraLensDirection.front, 270), CameraDescription(3, CameraLensDirection.front, 270)]
I have fixed these exceptions with a simple addition of a toEncodable() optional function passed to the JsonEncoder.withIndent() constructor. #88
List<CameraDescription> dumpThis = [ CameraDescription( name:"FrontOne", lensDirection:CameraLensDirection.front, sensorOrientation:0),
CameraDescription( name:"BackOne", lensDirection:CameraLensDirection.back, sensorOrientation:1),
CameraDescription( name:"ExternalOne", lensDirection:CameraLensDirection.external, sensorOrientation:2),
];
print( dumpThis );
logger.v( dumpThis );
Now outputs this (instead of exception):
I/flutter (11408): [CameraDescription(FrontOne, CameraLensDirection.front, 0), CameraDescription(BackOne, CameraLensDirection.back, 1), CameraDescription(ExternalOne, CameraLensDirection.external, 2)]
I/flutter (11408): ┌──────────────────────────────────────────────────
I/flutter (11408): │ [
I/flutter (11408): │ "CameraDescription(FrontOne, CameraLensDirection.front, 0)",
I/flutter (11408): │ "CameraDescription(BackOne, CameraLensDirection.back, 1)",
I/flutter (11408): │ "CameraDescription(ExternalOne, CameraLensDirection.external, 2)"
I/flutter (11408): │ ]
I/flutter (11408): └───────────────────────────────────────────────────