Git Product home page Git Product logo

Comments (9)

kevmoo avatar kevmoo commented on May 4, 2024

We recently added fromJson, toJson properties to JsonKey that let you provide methods to handle conversion yourself. You could detect if the value is a list or a single value and populate the property accordingly.

from json_serializable.dart.

fatherchill avatar fatherchill commented on May 4, 2024

Thanks, I appreciate the clarification. I know this isn't a help forum, but for the life of me I can't figure out what syntax I need to use with the fromJson and there isn't really any documentation for those functions. You mind helping me understand how I can use fromJson to handle either a list or a single item in the following case?

@JsonKey(name: 'book',)
 final List<Book> books;

 BooksContainer(
     this.books,
     );

from json_serializable.dart.

kevmoo avatar kevmoo commented on May 4, 2024

Something like

List<Book> _decodeBooks(Object input) {
  // your code here
}

class MyClass {
  @JsonKey(name: 'book', fromJson: _decodeBooks)
  final List<Book> books;
}

from json_serializable.dart.

fatherchill avatar fatherchill commented on May 4, 2024

from json_serializable.dart.

kevmoo avatar kevmoo commented on May 4, 2024

from json_serializable.dart.

fatherchill avatar fatherchill commented on May 4, 2024

I don't want to hastily open another issue. Yet after implementing fromJSON, debugging and running from Android Studio works fine, but release builds give a type mismatch error in my fromJSON method.

06-16 10:23:59.541 6695 6712 E flutter : [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
06-16 10:23:59.541 6695 6712 E flutter : type '_OneByteString' is not a subtype of type 'int' of 'index'
06-16 10:23:59.541 6695 6712 E flutter : #0 _GrowableList.[] (dart:core/runtime/libgrowable_array.dart:0)

Is there a problem in my code or is this an actual issue?

The offending method is below and here is the repository: https://github.com/fatherchill/world_liturgy_app/tree/CalendarAndCollects

List<dynamic> _decodePrayerBookorService(itemOrList){
//  debugPrint(itemOrList);
  print('begin decode Prayerbook or Service');

  List<dynamic> list = [];
  if (itemOrList == null){
    return null;
  }
  if (itemOrList.runtimeType.toString() == 'List<dynamic>'){
    list = itemOrList;
  }else{
    list.add(itemOrList);
  }

  print(list.toString().substring(0,20));

  if (list.first['service'] != null){
    return list.map((e) =>
    e == null ? null : new PrayerBook.fromJson(e as Map<String, dynamic>))
        ?.toList();
  }else if (list.first['section'] != null){
    return list.map((e) =>
    e == null ? null : new Service.fromJson(e as Map<String, dynamic>))
        ?.toList();
  }else{
    return null;
  }

}

from json_serializable.dart.

kevmoo avatar kevmoo commented on May 4, 2024
List<dynamic> list = [];
  if (itemOrList == null){
    return null;
  }
  if (itemOrList.runtimeType.toString() == 'List<dynamic>'){
    list = itemOrList;
  }else{
    list.add(itemOrList);
  }

Don't do that!

  if (itemOrList == null){
    return null;
  }
  List list;
  if (itemOrList.runtimeType.toString() == 'List<dynamic>'){
    list = itemOrList;
  }else{
    list = [itemOrList];
  }

Not sure if that'll help...but it's better...

from json_serializable.dart.

fatherchill avatar fatherchill commented on May 4, 2024

from json_serializable.dart.

a-wallen avatar a-wallen commented on May 4, 2024

For anyone else coming to this issue due to: More than one field has the JSON key for name <name>, you might be in this situation

class MyClass 
{
  SomeType A;

  @JsonKey(name: 'A')
  SomeType B;
}

It is possible if you are trying to rename a field on deserialization. You can solve it like this:

class MyClass 
{
  @JsonKey(ignore: true)
  SomeType A;

  @JsonKey(name: 'A')
  SomeType B;
}

from json_serializable.dart.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.