Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

publishedDate and Format Exception #18

Open
guidocimurro opened this issue Jul 5, 2022 · 1 comment · May be fixed by #19
Open

publishedDate and Format Exception #18

guidocimurro opened this issue Jul 5, 2022 · 1 comment · May be fixed by #19
Labels
bug Something isn't working

Comments

@guidocimurro
Copy link

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

@guidocimurro guidocimurro changed the title publishedDate publishedDate and Format Exception Jul 5, 2022
@guidocimurro
Copy link
Author

guidocimurro commented Jul 5, 2022

I'm really sorry for double posting... maybe something like this

DateTime? publishedDate = DateTime.tryParse((json['publishedDate'] as String?) ?? '0000-00-00');
    if (publishedDate == null) {
      final year = int.tryParse(json['publishedDate']);
      if (year != null) {
        publishedDate = DateTime(year);
      }
    }

because the case "yyyy-mm-dd" is already correctly handled by the DateTime.tryParse.

@bdlukaa bdlukaa added the bug Something isn't working label Jul 10, 2022
@bdlukaa bdlukaa linked a pull request Jul 17, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants