Home > Net >  The getter 'name' isn't defined for the type 'StatefulWidget' flutter
The getter 'name' isn't defined for the type 'StatefulWidget' flutter

Time:05-03

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);
  }

CodePudding user response:

You can use widget.variableName in the state class when variableName is in the initial class, but here your name is in a completly different class, so you can access it using widget.name from OtpUserState only if it was in OtpUser, but it's in Otpnewuser which is a completly different class.

CodePudding user response:

import the package that has (name : value) to your screen

like import 'package:project/model/project_model.dart';

  • Related