How to add a required parameter in flutter

Viewed 4808

I am creating an authentication page in flutter using this github repository. I have managed to get rid of most errors. I have a body.dart file that imports from other files one of them being background.dart that has a constructor with required parameters. Using a widget in body.dart brings up the error

The named parameter 'key' is required, but there's no corresponding argument.
Try adding the required argument.dart(missing_required_argument)

These are the code: background.dart

import 'package:flutter/material.dart';
class Background extends StatelessWidget {
  final Widget child;
  const Background({
    //added required to the key and child
    required Key key,
    required this.child,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return Container(
      height: size.height,
      width: double.infinity,
      child: Stack(
        alignment: Alignment.center,
        children: <Widget>[
          Positioned(
            top: 0,
            left: 0,
            child: Image.asset(
              "assets/images/main_top.png",
              width: size.width * 0.3,
            ),
          ),
          Positioned(
            bottom: 0,
            left: 0,
            child: Image.asset(
              "assets/images/main_bottom.png",
              width: size.width * 0.2,
            ),
          ),
          child,
        ],
      ),
    );
  }
}

body.dart

import 'package:flutter/material.dart';
import 'package:meekap/Screens/Welcome/components/background.dart';
import 'package:meekap/Screens/Login/login_screen.dart';
import 'package:meekap/Screens/Signup/signup_screen.dart';
import 'package:meekap/components/rounded_button.dart';
import '../../../constants.dart';

class Body extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    // This size provide us total height and width of our screen
    return Background(//Error comes up here
      child: SingleChildScrollView(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              "Welcome to meeKap",
              style: TextStyle(fontWeight: FontWeight.bold),
            ),
            SizedBox(height: size.height * 0.05),
            Image.asset(
              "assets/icons/chat.svg",
              height: size.height * 0.45,
            ),
            SizedBox(height: size.height * 0.05),
            RoundedButton(//Error comes up here
              text: "LOGIN",
              press: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) {
                      return LoginScreen();
                    },
                  ),
                );
              },
            ),
            RoundedButton(
              text: "SIGN UP",
              color: kPrimaryLightColor,
              textColor: Colors.black,
              press: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) {
                      return SignUpScreen();
                    },
                  ),
                );
              },
            ),
          ],
        ),
      ),
    );
  }
}


 
1 Answers

The key should not be required unless you have a reason to do so.

Remove required on the key parameter:

const Background({
    //added required to the key and child
    Key? key,
    required this.child,
  }) : super(key: key);
Related