Skip to content

Commit 9b72c51

Browse files
committed
Null-safety in Example
1 parent 4ec51a9 commit 9b72c51

File tree

5 files changed

+25
-22
lines changed

5 files changed

+25
-22
lines changed

example/lib/code_page.dart

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import 'package:flutter/material.dart';
2-
import 'package:widget_with_codeview/widget_with_codeview.dart';
2+
// import 'package:widget_with_codeview/widget_with_codeview.dart';
33

44
class CodePage extends StatefulWidget {
55
final String title;
66
final Widget child;
77
final String sourceFilePath;
88

99
const CodePage({
10-
Key key,
11-
@required this.title,
12-
@required this.child,
13-
@required this.sourceFilePath,
10+
Key? key,
11+
required this.title,
12+
required this.child,
13+
required this.sourceFilePath,
1414
}) : super(key: key);
1515

1616
@override
@@ -25,13 +25,15 @@ class _CodePageState extends State<CodePage> {
2525
title: Text(widget.title),
2626
elevation: 0,
2727
),
28-
body: WidgetWithCodeView(
28+
/*body: WidgetWithCodeView(
2929
child: widget.child,
3030
sourceFilePath: widget.sourceFilePath,
3131
// 1codeLinkPrefix` is optional. When it's specified, two more buttons
3232
// (open-code-in-browser, copy-code-link) will be added in the code view.
3333
// codeLinkPrefix: 'https://github.com/danvick/flutter_form_builder/blob/version_4/example/',
34-
),
34+
),*/
35+
body: widget.child,
3536
);
3637
}
3738
}
39+

example/lib/home_page.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class HomePage extends StatelessWidget {
1818
title: const Text('Complete Form'),
1919
trailing: const Icon(CupertinoIcons.right_chevron),
2020
onTap: () {
21-
return Navigator.of(context).push(
21+
Navigator.of(context).push(
2222
MaterialPageRoute(
2323
builder: (context) {
2424
return CodePage(
@@ -36,14 +36,15 @@ class HomePage extends StatelessWidget {
3636
title: const Text('Signup Form'),
3737
trailing: const Icon(CupertinoIcons.right_chevron),
3838
onTap: () {
39-
return Navigator.of(context).push(
39+
Navigator.of(context).push(
4040
MaterialPageRoute(
4141
builder: (context) {
4242
return CodePage(
4343
title: 'Signup Form',
4444
child: SignupForm(),
4545
sourceFilePath: 'lib/sources/signup_form.dart',
4646
);
47+
return SignupForm();
4748
},
4849
),
4950
);

example/lib/sources/complete_form.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class CompleteFormState extends State<CompleteForm> {
117117
onChanged: (val) {
118118
setState(() {
119119
_ageHasError =
120-
!_formKey.currentState.fields['age'].validate();
120+
!(_formKey.currentState?.fields['age']?.validate() ?? false);
121121
});
122122
},
123123
// valueTransformer: (text) => num.tryParse(text),
@@ -154,7 +154,7 @@ class CompleteFormState extends State<CompleteForm> {
154154
print(val);
155155
setState(() {
156156
_genderHasError =
157-
!_formKey.currentState.fields['gender'].validate();
157+
!(_formKey.currentState?.fields['gender']?.validate() ?? false);
158158
});
159159
},
160160
),
@@ -231,10 +231,10 @@ class CompleteFormState extends State<CompleteForm> {
231231
style: TextStyle(color: Colors.white),
232232
),
233233
onPressed: () {
234-
if (_formKey.currentState.saveAndValidate()) {
235-
print(_formKey.currentState.value);
234+
if (_formKey.currentState?.saveAndValidate() ?? false) {
235+
print(_formKey.currentState?.value);
236236
} else {
237-
print(_formKey.currentState.value);
237+
print(_formKey.currentState?.value);
238238
print('validation failed');
239239
}
240240
},
@@ -249,7 +249,7 @@ class CompleteFormState extends State<CompleteForm> {
249249
style: TextStyle(color: Theme.of(context).accentColor),
250250
),
251251
onPressed: () {
252-
_formKey.currentState.reset();
252+
_formKey.currentState?.reset();
253253
},
254254
),
255255
),

example/lib/sources/signup_form.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ class _SignupFormState extends State<SignupForm> {
5454
decoration: InputDecoration(
5555
labelText: 'Confirm Password',
5656
suffixIcon: (_formKey.currentState != null &&
57-
!_formKey.currentState.fields['confirm_password']
58-
.isValid)
57+
!(_formKey.currentState?.fields['confirm_password']
58+
?.isValid ?? false))
5959
? const Icon(Icons.error, color: Colors.red)
6060
: const Icon(Icons.check, color: Colors.green),
6161
),
@@ -68,7 +68,7 @@ class _SignupFormState extends State<SignupForm> {
6868
: null),*/
6969
(val) {
7070
if (val !=
71-
_formKey.currentState.fields['password'].value) {
71+
_formKey.currentState?.fields['password']?.value) {
7272
return 'Passwords do not match';
7373
}
7474
return null;
@@ -84,7 +84,7 @@ class _SignupFormState extends State<SignupForm> {
8484
]),
8585
// initialValue: true,
8686
decoration: InputDecoration(labelText: 'Accept Terms?'),
87-
builder: (FormFieldState<bool> field) {
87+
builder: (FormFieldState<bool?> field) {
8888
return InputDecorator(
8989
decoration: InputDecoration(
9090
errorText: field.errorText,
@@ -108,12 +108,12 @@ class _SignupFormState extends State<SignupForm> {
108108
style: TextStyle(color: Colors.white),
109109
),
110110
onPressed: () {
111-
if (_formKey.currentState.saveAndValidate()) {
111+
if (_formKey.currentState?.saveAndValidate() ?? false) {
112112
print('Valid');
113113
} else {
114114
print('Invalid');
115115
}
116-
print(_formKey.currentState.value);
116+
print(_formKey.currentState?.value);
117117
},
118118
)
119119
],

example/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ dependencies:
1111
sdk: flutter
1212
flutter_form_builder:
1313
path: ../
14-
widget_with_codeview: ^1.0.3
14+
# widget_with_codeview: ^1.0.5
1515
cupertino_icons: any
1616

1717
dependency_overrides:

0 commit comments

Comments
 (0)