Flutter HomeScreen(this.user) - Error: Too few positional arguments: 1 required

Viewed 112

good day my fellow coders I have a little problem where I am running firebase auth and cloud firestore in a flutter app with a curved nav bar. Now here is my problem on home_screen.dart file I had to add 3 things in order to get my crud function to work see code below:

class HomeScreen extends StatelessWidget {

  User user;
  HomeScreen(this.user);
  
  FirebaseFirestore firestore = FirebaseFirestore.instance;

  @override
  Widget build(BuildContext context) {
    return Scaffold(........

Now within my main_screen where my curved navigation bar is situated there is a _pageOption = [] there see below:

class _mainScreenState extends State<mainScreen> {

int _page = 0;

  final _pageOption = [

     add pages here

      HomeScreen(),
      AddCardScreen(),

  ];

Where i have the issue is everything is imported and works fine however when i pass this.user on the HomeScreen(), as it should be then I receive the Error:

Can't access 'this' in a field initializer.

1 Answers

Inside HomeScreen() you already specify the required parameter User user Because of that when you init HomeScreen its throws an error.

So for solution, you can set User in HomeScreen as null-check operator.

class HomeScreen extends StatelessWidget {

    User? user;  // null check operator ?
    HomeScreen(this.user);
}

Via this error will be cleared but before access user must check whether its null or not is mandatory.

Related