mirror of
https://github.com/flutter/samples.git
synced 2026-04-05 11:11:23 +00:00
Add flutter_lints to null_safety (#828)
This commit is contained in:
@@ -12,13 +12,15 @@ import 'services.dart';
|
||||
// uncomment the for-loop and appBar lines below, and note how the new null
|
||||
// safety static analysis immediately flags those lines as errors.
|
||||
class BadMyApp extends StatelessWidget {
|
||||
const BadMyApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final localizedAppName = Config.getAppName();
|
||||
final temperatures = WeatherService.getTemperatures();
|
||||
|
||||
var tempWidgets = [
|
||||
Text('Temperature next 3 days:'),
|
||||
const Text('Temperature next 3 days:'),
|
||||
// for (final t in temperatures) Text(t.round().toString()),
|
||||
];
|
||||
|
||||
|
||||
@@ -7,6 +7,8 @@ import 'package:flutter/material.dart';
|
||||
import 'services.dart';
|
||||
|
||||
class GoodMyApp extends StatelessWidget {
|
||||
const GoodMyApp({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final localizedAppName = Config.getAppName();
|
||||
@@ -14,13 +16,13 @@ class GoodMyApp extends StatelessWidget {
|
||||
|
||||
List<Widget> tempWidgets;
|
||||
if (temperatures == null) {
|
||||
tempWidgets = [Text('Temperature: Failed getting forecast :-(')];
|
||||
tempWidgets = [const Text('Temperature: Failed getting forecast :-(')];
|
||||
} else {
|
||||
// Null safety uses flow analysis. The code checked for null in the branch
|
||||
// above, so in this branch it known that temperatures cannot be null.
|
||||
// Notice how we access temperatures without getting an analysis error.
|
||||
tempWidgets = [
|
||||
Text('Temperature next 3 days:'),
|
||||
const Text('Temperature next 3 days:'),
|
||||
for (final t in temperatures)
|
||||
// We first use the conditional member access operator to only call
|
||||
// round() and toString() if t isn't null. We then test if that
|
||||
|
||||
@@ -7,5 +7,5 @@ import 'package:flutter/material.dart';
|
||||
import 'goodapp.dart';
|
||||
|
||||
void main() {
|
||||
runApp(GoodMyApp());
|
||||
runApp(const GoodMyApp());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user