1
0
mirror of https://github.com/flutter/samples.git synced 2026-04-05 19:22:08 +00:00

veggieseasons: Migrate to null safety (#922)

This commit is contained in:
Brett Morgan
2021-10-12 10:27:54 +11:00
committed by GitHub
parent 994cdb4afa
commit ac2bef7d83
21 changed files with 134 additions and 148 deletions

View File

@@ -26,22 +26,22 @@ class AppState extends ChangeNotifier {
return _veggies.where((v) => !v.seasons.contains(currentSeason)).toList();
}
Veggie getVeggie(int id) => _veggies.singleWhere((v) => v.id == id);
Veggie getVeggie(int? id) => _veggies.singleWhere((v) => v.id == id);
List<Veggie> searchVeggies(String terms) => _veggies
.where((v) => v.name.toLowerCase().contains(terms.toLowerCase()))
List<Veggie> searchVeggies(String? terms) => _veggies
.where((v) => v.name.toLowerCase().contains(terms!.toLowerCase()))
.toList();
void setFavorite(int id, bool isFavorite) {
void setFavorite(int? id, bool isFavorite) {
var veggie = getVeggie(id);
veggie.isFavorite = isFavorite;
notifyListeners();
}
/// Used in tests to set the season independent of the current date.
static Season debugCurrentSeason;
static Season? debugCurrentSeason;
static Season _getSeasonForDate(DateTime date) {
static Season? _getSeasonForDate(DateTime date) {
if (debugCurrentSeason != null) {
return debugCurrentSeason;
}

View File

@@ -16,7 +16,7 @@ class Preferences extends ChangeNotifier {
static const _preferredCategoriesKey = 'preferredCategories';
// Indicates whether a call to [_loadFromSharedPrefs] is in progress;
Future<void> _loading;
Future<void>? _loading;
int _desiredCalories = 2000;
@@ -79,9 +79,7 @@ class Preferences extends ChangeNotifier {
if (names != null && names.isNotEmpty) {
for (final name in names.split(',')) {
final index = int.tryParse(name) ?? -1;
if (VeggieCategory.values[index] != null) {
_preferredCategories.add(VeggieCategory.values[index]);
}
_preferredCategories.add(VeggieCategory.values[index]);
}
}

View File

@@ -3,7 +3,6 @@
// found in the LICENSE file.
import 'package:flutter/cupertino.dart';
import 'package:meta/meta.dart';
enum VeggieCategory {
allium,
@@ -71,18 +70,18 @@ const Map<Season, String> seasonNames = {
class Veggie {
Veggie({
@required this.id,
@required this.name,
@required this.imageAssetPath,
@required this.category,
@required this.shortDescription,
@required this.accentColor,
@required this.seasons,
@required this.vitaminAPercentage,
@required this.vitaminCPercentage,
@required this.servingSize,
@required this.caloriesPerServing,
@required this.trivia,
required this.id,
required this.name,
required this.imageAssetPath,
required this.category,
required this.shortDescription,
required this.accentColor,
required this.seasons,
required this.vitaminAPercentage,
required this.vitaminCPercentage,
required this.servingSize,
required this.caloriesPerServing,
required this.trivia,
this.isFavorite = false,
});
@@ -127,5 +126,5 @@ class Veggie {
/// A set of trivia questions and answers related to the veggie.
final List<Trivia> trivia;
String get categoryName => veggieCategoryNames[category];
String? get categoryName => veggieCategoryNames[category];
}