mirror of
https://github.com/flutter/samples.git
synced 2025-11-08 22:09:06 +00:00
Add flutter_lints to null_safety (#828)
This commit is contained in:
19
null_safety/null_safe_app/analysis_options.yaml
Normal file
19
null_safety/null_safe_app/analysis_options.yaml
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
include: package:flutter_lints/flutter.yaml
|
||||||
|
|
||||||
|
analyzer:
|
||||||
|
strong-mode:
|
||||||
|
implicit-casts: false
|
||||||
|
implicit-dynamic: false
|
||||||
|
|
||||||
|
linter:
|
||||||
|
rules:
|
||||||
|
avoid_types_on_closure_parameters: true
|
||||||
|
avoid_void_async: true
|
||||||
|
cancel_subscriptions: true
|
||||||
|
close_sinks: true
|
||||||
|
directives_ordering: true
|
||||||
|
package_api_docs: true
|
||||||
|
package_prefixed_library_names: true
|
||||||
|
test_types_in_equals: true
|
||||||
|
throw_in_finally: true
|
||||||
|
unnecessary_statements: true
|
||||||
@@ -12,13 +12,15 @@ import 'services.dart';
|
|||||||
// uncomment the for-loop and appBar lines below, and note how the new null
|
// uncomment the for-loop and appBar lines below, and note how the new null
|
||||||
// safety static analysis immediately flags those lines as errors.
|
// safety static analysis immediately flags those lines as errors.
|
||||||
class BadMyApp extends StatelessWidget {
|
class BadMyApp extends StatelessWidget {
|
||||||
|
const BadMyApp({Key? key}) : super(key: key);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final localizedAppName = Config.getAppName();
|
final localizedAppName = Config.getAppName();
|
||||||
final temperatures = WeatherService.getTemperatures();
|
final temperatures = WeatherService.getTemperatures();
|
||||||
|
|
||||||
var tempWidgets = [
|
var tempWidgets = [
|
||||||
Text('Temperature next 3 days:'),
|
const Text('Temperature next 3 days:'),
|
||||||
// for (final t in temperatures) Text(t.round().toString()),
|
// for (final t in temperatures) Text(t.round().toString()),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ import 'package:flutter/material.dart';
|
|||||||
import 'services.dart';
|
import 'services.dart';
|
||||||
|
|
||||||
class GoodMyApp extends StatelessWidget {
|
class GoodMyApp extends StatelessWidget {
|
||||||
|
const GoodMyApp({Key? key}) : super(key: key);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final localizedAppName = Config.getAppName();
|
final localizedAppName = Config.getAppName();
|
||||||
@@ -14,13 +16,13 @@ class GoodMyApp extends StatelessWidget {
|
|||||||
|
|
||||||
List<Widget> tempWidgets;
|
List<Widget> tempWidgets;
|
||||||
if (temperatures == null) {
|
if (temperatures == null) {
|
||||||
tempWidgets = [Text('Temperature: Failed getting forecast :-(')];
|
tempWidgets = [const Text('Temperature: Failed getting forecast :-(')];
|
||||||
} else {
|
} else {
|
||||||
// Null safety uses flow analysis. The code checked for null in the branch
|
// 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.
|
// above, so in this branch it known that temperatures cannot be null.
|
||||||
// Notice how we access temperatures without getting an analysis error.
|
// Notice how we access temperatures without getting an analysis error.
|
||||||
tempWidgets = [
|
tempWidgets = [
|
||||||
Text('Temperature next 3 days:'),
|
const Text('Temperature next 3 days:'),
|
||||||
for (final t in temperatures)
|
for (final t in temperatures)
|
||||||
// We first use the conditional member access operator to only call
|
// We first use the conditional member access operator to only call
|
||||||
// round() and toString() if t isn't null. We then test if that
|
// 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';
|
import 'goodapp.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(GoodMyApp());
|
runApp(const GoodMyApp());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,11 +62,25 @@ packages:
|
|||||||
description: flutter
|
description: flutter
|
||||||
source: sdk
|
source: sdk
|
||||||
version: "0.0.0"
|
version: "0.0.0"
|
||||||
|
flutter_lints:
|
||||||
|
dependency: "direct dev"
|
||||||
|
description:
|
||||||
|
name: flutter_lints
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "1.0.3"
|
||||||
flutter_test:
|
flutter_test:
|
||||||
dependency: "direct dev"
|
dependency: "direct dev"
|
||||||
description: flutter
|
description: flutter
|
||||||
source: sdk
|
source: sdk
|
||||||
version: "0.0.0"
|
version: "0.0.0"
|
||||||
|
lints:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: lints
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "1.0.1"
|
||||||
matcher:
|
matcher:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
name: null_safe_app
|
name: null_safe_app
|
||||||
description: A new Flutter project.
|
description: A new Flutter project.
|
||||||
publish_to: 'none' # Do not publish apps & package using the null safety experiment.
|
publish_to: "none" # Do not publish apps & package using the null safety experiment.
|
||||||
version: 1.2.0
|
version: 1.2.0
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
@@ -15,6 +15,7 @@ dependencies:
|
|||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
|
flutter_lints: ^1.0.0
|
||||||
|
|
||||||
flutter:
|
flutter:
|
||||||
uses-material-design: true
|
uses-material-design: true
|
||||||
|
|||||||
@@ -4,9 +4,9 @@ import 'package:flutter_test/flutter_test.dart';
|
|||||||
import 'package:null_safe_app/goodapp.dart';
|
import 'package:null_safe_app/goodapp.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
testWidgets('App smoke test', (WidgetTester tester) async {
|
testWidgets('App smoke test', (tester) async {
|
||||||
// Build our app and trigger a frame.
|
// Build our app and trigger a frame.
|
||||||
await tester.pumpWidget(GoodMyApp());
|
await tester.pumpWidget(const GoodMyApp());
|
||||||
|
|
||||||
// Verify that we have a forecast, or a managed failure.
|
// Verify that we have a forecast, or a managed failure.
|
||||||
expect(find.textContaining('Temperature'), findsOneWidget);
|
expect(find.textContaining('Temperature'), findsOneWidget);
|
||||||
|
|||||||
19
null_safety/null_unsafe_app/analysis_options.yaml
Normal file
19
null_safety/null_unsafe_app/analysis_options.yaml
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
include: package:flutter_lints/flutter.yaml
|
||||||
|
|
||||||
|
analyzer:
|
||||||
|
strong-mode:
|
||||||
|
implicit-casts: false
|
||||||
|
implicit-dynamic: false
|
||||||
|
|
||||||
|
linter:
|
||||||
|
rules:
|
||||||
|
avoid_types_on_closure_parameters: true
|
||||||
|
avoid_void_async: true
|
||||||
|
cancel_subscriptions: true
|
||||||
|
close_sinks: true
|
||||||
|
directives_ordering: true
|
||||||
|
package_api_docs: true
|
||||||
|
package_prefixed_library_names: true
|
||||||
|
test_types_in_equals: true
|
||||||
|
throw_in_finally: true
|
||||||
|
unnecessary_statements: true
|
||||||
@@ -5,13 +5,15 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(MyApp());
|
runApp(const MyApp());
|
||||||
}
|
}
|
||||||
|
|
||||||
// This app simulates possible null errors. Try running it and see if it fails.
|
// 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
|
// You can then try to hot reload a few times; you should see it occasionally
|
||||||
// failing and occasionally succeeding.
|
// failing and occasionally succeeding.
|
||||||
class MyApp extends StatelessWidget {
|
class MyApp extends StatelessWidget {
|
||||||
|
const MyApp({Key key}) : super(key: key);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
// Get data from services. Note: in a real application,
|
// Get data from services. Note: in a real application,
|
||||||
@@ -28,7 +30,7 @@ class MyApp extends StatelessWidget {
|
|||||||
child: Column(
|
child: Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
Text('Temperature next 3 days:'),
|
const Text('Temperature next 3 days:'),
|
||||||
for (final t in temperatures) Text(t.round().toString()),
|
for (final t in temperatures) Text(t.round().toString()),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -62,11 +62,25 @@ packages:
|
|||||||
description: flutter
|
description: flutter
|
||||||
source: sdk
|
source: sdk
|
||||||
version: "0.0.0"
|
version: "0.0.0"
|
||||||
|
flutter_lints:
|
||||||
|
dependency: "direct dev"
|
||||||
|
description:
|
||||||
|
name: flutter_lints
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "1.0.3"
|
||||||
flutter_test:
|
flutter_test:
|
||||||
dependency: "direct dev"
|
dependency: "direct dev"
|
||||||
description: flutter
|
description: flutter
|
||||||
source: sdk
|
source: sdk
|
||||||
version: "0.0.0"
|
version: "0.0.0"
|
||||||
|
lints:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: lints
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "1.0.1"
|
||||||
matcher:
|
matcher:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
name: null_unsafe_app
|
name: null_unsafe_app
|
||||||
description: A new Flutter project.
|
description: A new Flutter project.
|
||||||
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
|
publish_to: "none" # Remove this line if you wish to publish to pub.dev
|
||||||
version: 1.0.0+1
|
version: 1.0.0+1
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
@@ -14,5 +14,7 @@ dependencies:
|
|||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
|
flutter_lints: ^1.0.0
|
||||||
|
|
||||||
flutter:
|
flutter:
|
||||||
uses-material-design: true
|
uses-material-design: true
|
||||||
|
|||||||
@@ -8,8 +8,7 @@
|
|||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
testWidgets('Rendering the page throws an exception',
|
testWidgets('Rendering the page throws an exception', (tester) async {
|
||||||
(WidgetTester tester) async {
|
|
||||||
// Do nothing, running the app throws an exception on widget render.
|
// Do nothing, running the app throws an exception on widget render.
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user