mirror of
https://github.com/flutter/samples.git
synced 2025-11-08 22:09:06 +00:00
Migrate form_app to null safety (#925)
This commit is contained in:
@@ -6,7 +6,7 @@ import 'package:english_words/english_words.dart' as english_words;
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class FormValidationDemo extends StatefulWidget {
|
||||
const FormValidationDemo({Key key}) : super(key: key);
|
||||
const FormValidationDemo({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
_FormValidationDemoState createState() => _FormValidationDemoState();
|
||||
@@ -14,9 +14,9 @@ class FormValidationDemo extends StatefulWidget {
|
||||
|
||||
class _FormValidationDemoState extends State<FormValidationDemo> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
String adjective;
|
||||
String noun;
|
||||
bool agreedToTerms = false;
|
||||
String? adjective;
|
||||
String? noun;
|
||||
bool? agreedToTerms = false;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -32,7 +32,7 @@ class _FormValidationDemoState extends State<FormValidationDemo> {
|
||||
onPressed: () {
|
||||
// Validate the form by getting the FormState from the GlobalKey
|
||||
// and calling validate() on it.
|
||||
var valid = _formKey.currentState.validate();
|
||||
var valid = _formKey.currentState!.validate();
|
||||
if (!valid) {
|
||||
return;
|
||||
}
|
||||
@@ -69,7 +69,7 @@ class _FormValidationDemoState extends State<FormValidationDemo> {
|
||||
autofocus: true,
|
||||
textInputAction: TextInputAction.next,
|
||||
validator: (value) {
|
||||
if (value.isEmpty) {
|
||||
if (value!.isEmpty) {
|
||||
return 'Please enter an adjective.';
|
||||
}
|
||||
if (english_words.adjectives.contains(value)) {
|
||||
@@ -92,7 +92,7 @@ class _FormValidationDemoState extends State<FormValidationDemo> {
|
||||
// A text field that validates that the text is a noun.
|
||||
TextFormField(
|
||||
validator: (value) {
|
||||
if (value.isEmpty) {
|
||||
if (value!.isEmpty) {
|
||||
return 'Please enter a noun.';
|
||||
}
|
||||
if (english_words.nouns.contains(value)) {
|
||||
@@ -151,7 +151,7 @@ class _FormValidationDemoState extends State<FormValidationDemo> {
|
||||
formFieldState.errorText ?? "",
|
||||
style: Theme.of(context)
|
||||
.textTheme
|
||||
.caption
|
||||
.caption!
|
||||
.copyWith(color: Theme.of(context).errorColor),
|
||||
),
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user