blob: 075b825b4737dacc907e562d0b706f82a55068e7 [file] [log] [blame]
// Copyright 2013 The Flutter Authors. 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';
/// Library data mock.
final Library libraryInstance = Library()
title: 'Left Hand of Darkness',
authorName: 'Ursula K. Le Guin',
isPopular: true,
isNew: true)
title: 'Too Like the Lightning',
authorName: 'Ada Palmer',
isPopular: false,
isNew: true)
title: 'Kindred',
authorName: 'Octavia E. Butler',
isPopular: true,
isNew: false)
title: 'The Lathe of Heaven',
authorName: 'Ursula K. Le Guin',
isPopular: false,
isNew: false);
/// A library that contains books and authors.
class Library {
/// The books in the library.
final List<Book> allBooks = <Book>[];
/// The authors in the library.
final List<Author> allAuthors = <Author>[];
/// Adds a book into the library.
void addBook({
required String title,
required String authorName,
required bool isPopular,
required bool isNew,
}) {
final Author author = allAuthors.firstWhere(
(Author author) => == authorName,
orElse: () {
final Author value = Author(id: allAuthors.length, name: authorName);
return value;
final Book book = Book(
id: allBooks.length,
title: title,
isPopular: isPopular,
isNew: isNew,
author: author,
/// The list of popular books in the library.
List<Book> get popularBooks => <Book>[
...allBooks.where((Book book) => book.isPopular),
/// The list of new books in the library.
List<Book> get newBooks => <Book>[
...allBooks.where((Book book) => book.isNew),