Thanks to everyone for there help on my previous question.
Using Flutter ver 3.3.8 and Android Studio Dolphin | 2021.3.1 Patch 1 (This is the latest version that i'm using. Since the book is using an older version.)
Modified the Widget with this code see below
Still having issues with this line of code -
if (inValue?.isEmpty) {
return "Please enter username";
}
Flutter shows this msg - A nullable expression can't be used as a condition. Try checking that the value isn't 'null' before using it as a condition. See screenshot of the message listed below. A;so put the full code below the screenshot.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
_MyApp createState() => _MyApp();
}
class LoginData {
String username = "";
String password = "";
}
class _MyApp extends State {
LoginData loginData = LoginData();
final GlobalKey<FormState> _formkey = GlobalKey<FormState>();
@override
Widget build(BuildContext inContext) {
return MaterialApp(home: Scaffold(
body : Container(
padding : const EdgeInsets.all(50.0),
child : Form(
key: _formkey,
child: Column(
children: [
TextFormField(
keyboardType: TextInputType.emailAddress,
validator: (String? inValue) {
if (inValue?.isEmpty) {
return "Please enter username";
}
return null;
},
onSaved: (String inValue) {
loginData.username = inValue;
},
decoration: const InputDecoration(
hintText: "[email protected]",
labelText: "Username (eMail address)"
),
),
TextFormField(
obscureText: true,
validator: (String inValue) {
if (inValue.length < 10) {
return "Password must be >=10 in length";
}
return null;
},
onSaved: (String inValue) {
this.loginData.password = inValue;
},
decoration: InputDecoration(
hintText : "Password",
labelText : "Password"
),
),
ElevatedButton(
child : Text("Log In!"),
onPressed: (){
if (_formkey.currentState.validate()){
_formkey.currentState.save();
print ("Username: ${_loginData.username}");
print ("Password: ${_loginData.password}");
}
},
),
]
),
),
),
));
}
}
Thanks to everyone for there help on my previous question.
Using Flutter ver 3.3.8 and Android Studio Dolphin | 2021.3.1 Patch 1 (This is the latest version that i'm using. Since the book is using an older version.)
Modified the Widget with this code see below
Still having issues with this line of code -
if (inValue?.isEmpty) {
return "Please enter username";
}
Flutter shows this msg - A nullable expression can't be used as a condition. Try checking that the value isn't 'null' before using it as a condition. See screenshot of the message listed below. A;so put the full code below the screenshot.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
_MyApp createState() => _MyApp();
}
class LoginData {
String username = "";
String password = "";
}
class _MyApp extends State {
LoginData loginData = LoginData();
final GlobalKey<FormState> _formkey = GlobalKey<FormState>();
@override
Widget build(BuildContext inContext) {
return MaterialApp(home: Scaffold(
body : Container(
padding : const EdgeInsets.all(50.0),
child : Form(
key: _formkey,
child: Column(
children: [
TextFormField(
keyboardType: TextInputType.emailAddress,
validator: (String? inValue) {
if (inValue?.isEmpty) {
return "Please enter username";
}
return null;
},
onSaved: (String inValue) {
loginData.username = inValue;
},
decoration: const InputDecoration(
hintText: "[email protected]",
labelText: "Username (eMail address)"
),
),
TextFormField(
obscureText: true,
validator: (String inValue) {
if (inValue.length < 10) {
return "Password must be >=10 in length";
}
return null;
},
onSaved: (String inValue) {
this.loginData.password = inValue;
},
decoration: InputDecoration(
hintText : "Password",
labelText : "Password"
),
),
ElevatedButton(
child : Text("Log In!"),
onPressed: (){
if (_formkey.currentState.validate()){
_formkey.currentState.save();
print ("Username: ${_loginData.username}");
print ("Password: ${_loginData.password}");
}
},
),
]
),
),
),
));
}
}