mirror of
https://github.com/flutter/samples.git
synced 2025-11-11 23:39:14 +00:00
Add a new null safety example (#562)
This commit is contained in:
34
experimental/null_safety/null_safe_app/lib/services.dart
Normal file
34
experimental/null_safety/null_safe_app/lib/services.dart
Normal file
@@ -0,0 +1,34 @@
|
||||
// Copyright 2020, 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.
|
||||
|
||||
class Config {
|
||||
static String? getAppName() {
|
||||
// Imagine this looks up a localized version of the app name. We're using
|
||||
// the current time to simulate a variance in responses.
|
||||
if (DateTime.now().second.isEven) {
|
||||
return 'Weather forecast';
|
||||
} else {
|
||||
// Oops, we don't have a localization.
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class WeatherService {
|
||||
static List<double?>? getTemperatures() {
|
||||
// Imagine this makes a network call to get the current temperature.
|
||||
// We're using the current time to simulate a variance in responses.
|
||||
if (DateTime.now().millisecond.isEven) {
|
||||
return [32.2, 34.5, 31.0];
|
||||
} else {
|
||||
if ((DateTime.now().second / 10).round().isEven) {
|
||||
// Oops, we couldn't get any temperatures.
|
||||
return null;
|
||||
} else {
|
||||
// Oops, we couldn't get one of the temperatures.
|
||||
return [32.2, 34.5, null, 31.0];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user