The getter 'name' isn't defined for the type 'StatefulWidget'

Viewed 278

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'

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

Check the example in the official docs once again. The property name needs to be defined the in the class which extends StatefulWidget, but in your code it is not.

Here is the example:

class Bird extends StatefulWidget {
  const Bird({
    Key? key,
    this.color = const Color(0xFFFFE306),
    this.child,
  }) : super(key: key);

  final Color color;
  final Widget? child;

  @override
  State<Bird> createState() => _BirdState();
}

class _BirdState extends State<Bird> {
  double _size = 1.0;

  void grow() {
    setState(() { _size += 0.1; });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: widget.color,
      transform: Matrix4.diagonal3Values(_size, _size, 1.0),
      child: widget.child,
    );
  }
}

Link: https://api.flutter.dev/flutter/widgets/StatefulWidget-class.html

Notice how both color and child are defined in Bird which extends StatefulWidget. This way you can use them inside your actual state (widget.color and widget.child)

So from what i see is you are using a stateless widget Otpnewuser class and then you are using the OtpUser that is a statefull widget and you are trying to do

trying to access widget.name in OtpUser statefull class which is not defined.

This is not possible for your code as you need to pass the name from constructor OtpUser which will eventually give you the acces to name string.

I have added a sample example below check it out and run it you will know the working.

import 'package:flutter/material.dart';

void main() {
  runApp(const OtpNewUser(
    name: "sample name",
  ));
}

class OtpNewUser extends StatelessWidget {
  final String name;
  const 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: Center(child: OtpUser(
            name: name,
          )),

        ));
  }
}


class OtpUser extends StatefulWidget {
  final String name;
  const OtpUser({Key? key, required this.name}) : super(key: key);

  @override
  _OtpUserState createState() => _OtpUserState();
}

class _OtpUserState extends State<OtpUser> {

  @override
  void initState() {

    super.initState();

    print(widget.name);


  }


  @override
  Widget build(BuildContext context) {
    return Container();
  }
}



Do let me know if it works

Related