1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-11 15:28:44 +00:00
Files
samples/navigation_and_routing/lib/src/data/library.dart
John Ryan 49eebf8c20 Update navigation_and_routing sample to go_router (#2067)
This is a new PR to update the navigation_and_routing sample to the
latest version of go_router. It is a based on
https://github.com/flutter/samples/pull/1437

---------

Co-authored-by: Brett Morgan <brettmorgan@google.com>
2023-11-01 12:14:00 -07:00

66 lines
1.6 KiB
Dart

// Copyright 2021, the Flutter project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'author.dart';
import 'book.dart';
final libraryInstance = Library()
..addBook(
title: 'Left Hand of Darkness',
authorName: 'Ursula K. Le Guin',
isPopular: true,
isNew: true)
..addBook(
title: 'Too Like the Lightning',
authorName: 'Ada Palmer',
isPopular: false,
isNew: true)
..addBook(
title: 'Kindred',
authorName: 'Octavia E. Butler',
isPopular: true,
isNew: false)
..addBook(
title: 'The Lathe of Heaven',
authorName: 'Ursula K. Le Guin',
isPopular: false,
isNew: false);
class Library {
final List<Book> allBooks = [];
final List<Author> allAuthors = [];
void addBook({
required String title,
required String authorName,
required bool isPopular,
required bool isNew,
}) {
var author = allAuthors.firstWhere(
(author) => author.name == authorName,
orElse: () {
final value = Author(allAuthors.length, authorName);
allAuthors.add(value);
return value;
},
);
var book = Book(allBooks.length, title, isPopular, isNew, author);
author.books.add(book);
allBooks.add(book);
}
Book getBook(String id) {
return allBooks[int.parse(id)];
}
List<Book> get popularBooks => [
...allBooks.where((book) => book.isPopular),
];
List<Book> get newBooks => [
...allBooks.where((book) => book.isNew),
];
}