mirror of
https://github.com/flutter/samples.git
synced 2025-11-10 23:08:59 +00:00
Upgrade null safety example (#748)
This commit is contained in:
70
null_safety/null_unsafe_app/lib/main.dart
Normal file
70
null_safety/null_unsafe_app/lib/main.dart
Normal file
@@ -0,0 +1,70 @@
|
||||
// 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.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() {
|
||||
runApp(MyApp());
|
||||
}
|
||||
|
||||
// This app simulates possible null errors. Try running it and see if it fails.
|
||||
// You can then try to hot reload a few times; you should see it occasionally
|
||||
// failing and occasionally succeeding.
|
||||
class MyApp extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// Get data from services. Note: in a real application,
|
||||
// these would be async calls, but we’re using sync calls
|
||||
// for simplicity.
|
||||
final localizedAppName = Config.getAppName();
|
||||
final temperatures = WeatherService.getTemperatures();
|
||||
|
||||
return MaterialApp(
|
||||
home: Scaffold(
|
||||
appBar: AppBar(title: Text(localizedAppName)),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text('Temperature next 3 days:'),
|
||||
for (final t in temperatures) Text(t.round().toString()),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
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