I am getting this error, can't print the value in name. Why do I get errors like that here? The getter 'name' isn't defined for the type 'StatefulWidget'. Try importing the library that defines 'name', correcting the name to the name of an existing getter, or defining a getter or field named 'name'
error in print(widget.name);
class Otpnewuser extends StatelessWidget {
String name;
Otpnewuser({Key key, @required this.name}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Image.asset('assets/logo-sm.jpg', fit: BoxFit.cover),
backgroundColor: const Color.fromARGB(255, 111, 70, 173),
centerTitle: true),
body: const Center(child: OtpUser()),
));
}
}
class OtpUser extends StatefulWidget {
const OtpUser({Key key}) : super(key: key);
@override
OtpUserState createState() => OtpUserState();
}
class OtpUserState extends State {
void method() {
print(widget.name);
}
You can use
widget.variableNamein the state class whenvariableNameis in the initial class, but here your name is in a completly different class, so you can access it usingwidget.namefromOtpUserStateonly if it was inOtpUser, but it's inOtpnewuserwhich is a completly different class.