Skip to content

publishedDate and Format Exception #18

@guidocimurro

Description

@guidocimurro

If you try to search "google"

final List<Book> books = await queryBooks( 'google', maxResults: 3, printType: PrintType.books, orderBy: OrderBy.relevance, reschemeImageLinks: true, );

Link to web query

the second result is a book that has a different publishedDate format, i think ISO (2016-09-15T00:00:00+02:00) and it raises a format exception at books.dart line 241

day = int.parse(publishedDateArray[2]);

I think you can try to parse the date with something like

`DateTime? publishedDate =
DateTime.tryParse((json['publishedDate'] as String?) ?? '0000-00-00');

if (publishedDate == null) {
  final publishedDateArray =
      ((json['publishedDate'] as String?) ?? '0000-00-00').split('-');

  // initialize datetime variable

  if (publishedDateArray.isNotEmpty) {
    // initialize date
    int year = int.parse(publishedDateArray[0]);
    int month = 1;
    int day = 1;

    // now test the date string
    if (publishedDateArray.length == 1) {
      // assume we have only the year
      year = int.parse(publishedDateArray[0]);
    }
    if (publishedDateArray.length == 2) {
      // assume we have the year and maybe the month (this could be just a speculative case)
      year = int.parse(publishedDateArray[0]);
      month = int.parse(publishedDateArray[1]);
    }
    if (publishedDateArray.length == 3) {
      // assume we have year-month-day
      year = int.parse(publishedDateArray[0]);
      month = int.parse(publishedDateArray[1]);
      day = int.parse(publishedDateArray[2]);
    }
    publishedDate = DateTime(year, month, day);
  }
}`

If I can help in any mode please tell me (but I'm NOT a professional developer).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions