Yooni
Yooni

Reputation: 61

How can I solve Navigator.push() error in Flutter?

There are Error Messagge, Error Code and some HomePageCode. What is matter??

I did that error message said to do. But I CAN'T SOLVE PLOBLEM.

[Error Message]

lib/create_page.dart:194:63: Error: Too few positional arguments: 1 required, 0 given.
     context, MaterialPageRoute(builder:(context) => HomePage()),
                                                             ^
 lib/home_page.dart:10:3: Context: Found this candidate, but the arguments don't match.
     HomePage(this.user);
              ^^^^^^^^

[Error Code]

Navigator.push(
  context, MaterialPageRoute(builder:(context) => HomePage()),
);

[HomePage]

class HomePage extends StatelessWidget {
final FirebaseUser user;

HomePage(this.user);

Upvotes: 0

Views: 521

Answers (4)

ahmed
ahmed

Reputation: 122

HomePage class has a contractor so you should enter the right arguments.

if you want your argument to be optional you should make user can be null by using the:

final FirebaseUser? user;

i use Getx to handle Navigator and state manager

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ttest/screen/homepage.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
     
      home:Scaffold(
        floatingActionButton: FloatingActionButton(onPressed: (){
          Get.to(HomePage());

        }),
      ));
  }
}

and for HomePage file

import 'package:flutter/material.dart';

class HomePage extends StatelessWidget {

   final FirebaseUser? user;

  const HomePage({super.key, this.user});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
  appBar: AppBar(),
  backgroundColor: Colors.red,
);
  }
}

class FirebaseUser {
}

Upvotes: 0

Najedo
Najedo

Reputation: 1

This is because HomePage widget expects an argument of type FirebaseUser. You need to add this argument in the navigator

Navigator.push(
  context, MaterialPageRoute(builder:(context) => HomePage(user)),
);

Upvotes: 0

IonicFireBaseApp
IonicFireBaseApp

Reputation: 1230

final FirebaseUser user;

here user is required argument,which is causing the error, either you pass it or make it nullable like

final FirebaseUser? user;

Upvotes: 0

Md. Yeasin Sheikh
Md. Yeasin Sheikh

Reputation: 63569

As for the HomePage class, you need to pass a FirebaseUser instance. If you like to make it optional, you can use named argument constructor with nullable datatype.

class HomePage extends StatelessWidget {
  final FirebaseUser? user;

  const HomePage({super.key, this.user});
}

Upvotes: 1

Related Questions