The same code works in the tutorial video, not sure what is happening here. I think this is related to null safety.
I did try applying ? in this solution, but I was not able to reach a solution that works. When I apply the ? method, the validation wont' work.
How to solve this issue and build the project.
This is what I get when I run the solution

This is the video URL having this sample https://youtu.be/nFSL-CqwRDo

This is my full code
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
//const MyApp({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Coding with Curry',
theme: ThemeData(
primarySwatch: Colors.teal,
),
home: FormScreen(),
);
}
}
class FormScreen extends StatefulWidget {
//FormScreen({Key key}) : super(key: key);
@override
_FormScreenState createState() => _FormScreenState();
}
class _FormScreenState extends State<FormScreen> {
String _name = "";
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
Widget _buildName() {
return TextFormField(
decoration: InputDecoration(labelText: 'Name'),
validator: (String value) {
if (value.isEmpty) {
return "Name is Requried";
}
},
onSaved: (String value){
_name = value;
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Form Demo"),
),
body: Container(
margin: EdgeInsets.all(24),
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
_buildName(),
SizedBox(
height: 100,
),
RaisedButton(
child: Text(
"Submit",
style: TextStyle(color: Colors.blue, fontSize: 16),
),
onPressed: () => {
if(!_formKey.currentState.validate()){
return;
}
_formKey.currentState.save();
})
],
),
),
));
}
}
When I apply the null in the solution, I am not able to write the validation code
