logger icon indicating copy to clipboard operation
logger copied to clipboard

Fails to encode map.keys

Open jifalops opened this issue 4 years ago • 6 comments

Converting object to an encodable object failed: Instance of '_CompactIterable<String>'

print() had handled this without error.

jifalops avatar May 19 '20 20:05 jifalops

When did you run into this exception? Can you tell me which printers you were using? Also please provide the entire stacktrace/error output.

haarts avatar May 27 '20 09:05 haarts

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): E/flutter ( 7377): #11 ChatBloc.mapEventToState package:grovemobile/…/chat/chat_bloc.dart:50 E/flutter ( 7377): E/flutter ( 7377): #12 Bloc._bindEventsToStates. package:bloc/src/bloc.dart:252 E/flutter ( 7377): #13 Stream.asyncExpand.onListen. (dart:async/stream.dart:579:30) E/flutter ( 7377): #14 _rootRunUnary (dart:async/zone.dart:1192:38) E/flutter ( 7377): #15 _CustomZone.runUnary (dart:async/zone.dart:1085:19) E/flutter ( 7377): #16 _CustomZone.runUnaryGuarded (dart:async/zone.dart:987:7) E/flutter ( 7377): #17 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11) E/flutter ( 7377): #18 _DelayedData.perform (dart:async/stream_impl.dart:594:14) E/flutter ( 7377): #19 _StreamImplEvents.handleNext (dart:async/stream_impl.dart:710:11) E/flutter ( 7377): #20 _PendingEvents.schedule. (dart:async/stream_impl.dart:670:7) E/flutter ( 7377): #21 _rootRun (dart:async/zone.dart:1180:38) E/flutter ( 7377): #22 _CustomZone.run (dart:async/zone.dart:1077:19) E/flutter ( 7377): #23 _CustomZone.runGuarded (dart:async/zone.dart:979:7)

dvalles avatar Jun 18 '20 17:06 dvalles

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.

haarts avatar Sep 28 '20 09:09 haarts

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.

How ablout if Class don't have toString method, print Instance of 'xxxx' instead of throw Error.

boboidream avatar Oct 19 '20 08:10 boboidream

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)]

0xNF avatar Mar 09 '21 02:03 0xNF

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): └───────────────────────────────────────────────────

timmaffett avatar Mar 19 '21 21:03 timmaffett