skip to Main Content

I am new to Flutter/Dart and have some code that someone wrote for me awhile ago and I am just now trying to figure it out. In reviewing the application code, I run into this error issue quite a lot and I am trying to understand it. Can someone help me or point in the right direction.

import 'package:flutter/material.dart';

import '../../utils/color_palette.dart';
import 'circular_icon_button.dart';

class PositionedEditPictureButton extends StatelessWidget {
  final Widget child;
  final bool canEdit;
  final double bottomPosition;

  const PositionedEditPictureButton({
    required Key key,
    required this.child,
    this.canEdit = false,
    this.bottomPosition = 10.0,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Stack(
      alignment: Alignment.bottomCenter,
      children: <Widget>[
        this.child,
        if (canEdit)
          Positioned(
            bottom: this.bottomPosition,
            child: CircularIconButton(
              size: 40.0,
              icon: Icon(
                Icons.add_a_photo,
                size: 24,
                color: ColorPalette().mainBackground,
              ), key: key, color: Colors.black, onPressed: () {  }, //Here is the error line - The argument type 'Key?' can't be assigned to the parameter type 'Key'. 
            ),
          ),
      ],
    );
  }
}

I have taken some beginner flutter courses on Udemy so I am trying to walk my way through the code base. I can ‘t seem to figure out what I am missing.
I think if someone could help with this issue I could better understand how to help myself.

2

Answers


  1. Try this

    const PositionedEditPictureButton({
        super.key, 
        required this.child,
        this.canEdit = false,
        this.bottomPosition = 10.0,
      });
    
    Login or Signup to reply.
  2. We cannot see with the code you provided but I guess your CircularIconButton widget want a not nullable key, while the key you are passing seems to be nullable.

    You can tell if the key awaited by you CircularIconButton is nullable by checking its definition.
    If there is not question mark after the type, it means the CircularIconButton need a not nullable.

    Not nullable:

    final Key key;
    

    Nullable:

    final Key? key;
    

    Depending on what you do with this key inside CircularIconButton, you may or may not want to make it nullable.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search