Home > OS >  The named parameter 'user' is required, but there's no corresponding argument in flut
The named parameter 'user' is required, but there's no corresponding argument in flut

Time:04-28

It seems like my page1() requires a user property, i've tried passing the property and i still get an error Undefined name 'user'.Try correcting the name to one that is defined, or defining the name

My code

class Home extends StatefulWidget {
   final User user;

const Home({required this.user});

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

  class _HomeState extends State<Home> {
  int pageIndex = 0;

  final pages = [
  Page1(user: user),

 ];
 ...
 }

Page1

class Page1 extends StatefulWidget {
final User user;

 const Page1({required this.user});


 @override
 State<Page1> createState() => _Page1State();
 }

  class _Page1State extends State<Page1> {

 late User _currentUser;
 @override
  void initState() {
  _currentUser = widget.user;
  super.initState();
 }
  @override
  Widget build(BuildContext context) {
  return Container(
   ..
  
  );}}

RegisterPage

class RegisterPage extends StatefulWidget {
 @override
 _RegisterPageState createState() => _RegisterPageState();
 }

 class _RegisterPageState extends State<RegisterPage> {
 ..

  @override
  Widget build(BuildContext context) {
  return GestureDetector(
   onTap: () {
    ..
    },
     child: Scaffold(
      ElevatedButton(
            onPressed: () async {
                                  setState(() {
                                    _isProcessing = true;
                                  });

                                  if 
(_registerFormKey.currentState!
  .validate()) {
  User? user = await FireAuth
                                        
    .registerUsingEmailPassword(
    name: _nameTextController.text,
    email: _emailTextController.text,
    password:                                 
     _passwordTextController.text, );

                                    setState(() {
                                      _isProcessing = false;
                                    });

                                    if (user != null) {
                                      Navigator.of(context)
                                          .pushAndRemoveUntil(
                                        MaterialPageRoute(
                                          builder: (context) =>
                                              Home(user: user),
                                        ),
                                        ModalRoute.withName('/'),
                                      );
                                    }
                                  }
                                },
                                child: Text(
                                  'Sign up',
                                  style: TextStyle(color: Colors.white),
                                ),
                              ), )
  }

A user is created on sign up and then has to be navigated to the page1, everything else is working fine except the navigation part.

I've add the register page to show the user created that after signup is navigated to home() . Now i want to navigate to page1() from home()

CodePudding user response:

class _HomeState extends State<Home> {
  int pageIndex = 0;
  late User user;
  late final pages ;

  @override
  void initState() {
    user = widget.user;
    pages = [
      Page1(user:user),
    ],
    super.initState();
  }

  
  ...
}

I think you should change your code like this : user: widget.user

CodePudding user response:

This will navigate from Register page to Page 1

if (user != null) {
   Navigator.of(context)
       .pushAndRemoveUntil(
           MaterialPageRoute(
               builder: (context) =>
                  Page1(user: user),
           ),
           ModalRoute.withName('/'),
        );
}

Also, change this in Page1

class Page1 extends StatefulWidget {
final User user;

 const Page1({required this.user});

 //this must be State<Page1>
 @override
 State<Page1> createState() => _Page1State();
}
  • Related