0

Hello I try to pass image by constructor. But I don't want to add 'required' , because I don't need it in a whole list

Whole error - 'The parameter 'image' can't have a value of 'null' because of its type, but the implicit default value is 'null'. Try adding either an explicit non-'null' default value or the 'required' modifier'

the code where I create the constructor

class LvPopup extends StatelessWidget {
  final String title;
  final String users_info;
  final String image;
  LvPopup({
    super.key,
    required this.title,
    required this.users_info,
    this.image,
  });

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        children: [
          SizedBox(height: 10),
          Column(
            children: [
              Row(
                children: [
                  Text(
                    title,
                    style: TextStyle(color: PaidworkColors.lightTxtColor),
                  ),
                ],
              ),
              SizedBox(
                height: 5,
              ),
              Row(
                children: [
                  Image.asset(image),
                  Padding(
                    padding: const EdgeInsets.all(5.0),
                    child: Text(
                      users_info,
                    ),
                  ),
                ],
              )
            ],
          )
        ],
      ),
    );
  }
}

and here is the code where I try to pass LvPopup( image: 'assets/images/blue_dot.png', users_info: "In the process", title: 'Status', ),

I tried to add an question mark like this 'final String? image; ', and then in the image.asset(image as String) , then this error above disappear , and there is a new one 'Type Null is not a subtype of type 'string' in type cast'.

EL QONDZIX
  • 17
  • 4

1 Answers1

0
class LvPopup extends StatelessWidget {
  final String title;
  final String users_info;
  final String? image;
  LvPopup({
    super.key,
    required this.title,
    required this.users_info,
    this.image,
  });

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        children: [
          SizedBox(height: 10),
          Column(
            children: [
              Row(
                children: [
                  Text(
                    title,
                    style: TextStyle(color: PaidworkColors.lightTxtColor),
                  ),
                ],
              ),
              SizedBox(
                height: 5,
              ),
              Row(
                children: [
                  Image.asset(image ?? ""),
                  Padding(
                    padding: const EdgeInsets.all(5.0),
                    child: Text(
                      users_info,
                    ),
                  ),
                ],
              )
            ],
          )
        ],
      ),
    );
  }
}

or

class LvPopup extends StatelessWidget {
  final String title;
  final String users_info;
  final String? image;
  LvPopup({
    super.key,
    required this.title,
    required this.users_info,
    this.image,
  });

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        children: [
          SizedBox(height: 10),
          Column(
            children: [
              Row(
                children: [
                  Text(
                    title,
                    style: TextStyle(color: PaidworkColors.lightTxtColor),
                  ),
                ],
              ),
              SizedBox(
                height: 5,
              ),
              Row(
                children: [
                  Image.asset(image!),
                  Padding(
                    padding: const EdgeInsets.all(5.0),
                    child: Text(
                      users_info,
                    ),
                  ),
                ],
              )
            ],
          )
        ],
      ),
    );
  }
}

The first one will set Image.asset(image) to an empty string if image is null while the second will tell dart that image is not null

Edit: To address the issue where the image appears in other places, use this

class LvPopup extends StatelessWidget {
  final String title;
  final String users_info;
  final String? image;
  LvPopup({
    super.key,
    required this.title,
    required this.users_info,
    this.image,
  });

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        children: [
          SizedBox(height: 10),
          Column(
            children: [
              Row(
                children: [
                  Text(
                    title,
                    style: TextStyle(color: PaidworkColors.lightTxtColor),
                  ),
                ],
              ),
              SizedBox(
                height: 5,
              ),
              Row(
                children: [
                  image != null ? Image.asset(image!) : Container (),
                  Padding(
                    padding: const EdgeInsets.all(5.0),
                    child: Text(
                      users_info,
                    ),
                  ),
                ],
              )
            ],
          )
        ],
      ),
    );
  }
}
Frank nike
  • 330
  • 5
  • 12
  • What the problem? If it's different from this one you might have to open another question – Frank nike Jan 28 '23 at 12:52
  • There is another problem, because I have added more values like this: `` LvPopup( title: 'E-mail', users_info: 'gmail.com', ), LvPopup( users_info: 'Johny Bravo', title: 'Name and Surname', ), LvPopup( image: 'assets/images/blue_dot.png', users_info: "In the process ", title: 'Status', ), LvPopup( users_info: '+0.00', title: 'Earnings(USD)', ) `` and I get this https://imgur.com/a/owsoxsY – EL QONDZIX Jan 28 '23 at 12:55
  • That is different from this question. Check [Stack Overflow](https://stackoverflow.com/a/53752071/13534779). Or if you're sure you did everything correctly, restart/rebuild your app. – Frank nike Jan 28 '23 at 12:57
  • But the problem is that it added automatically image to the rest of LvPopup , I want the Image only in: ``` LvPopup( image: 'assets/images/blue_dot.png', users_info: "In the process ", title: 'Status', ), ``` no idea why only here i want it https://imgur.com/a/SMY12zi and I dont know why it added any assets to this: https://imgur.com/a/KWM8Hdy when I passed only title and users info to that one https://imgur.com/a/6AA8vNN – EL QONDZIX Jan 28 '23 at 13:02
  • If it added `LvPopup` it simply means you're using the same class instance for others. – Frank nike Jan 28 '23 at 13:07
  • I create new question – EL QONDZIX Jan 28 '23 at 13:08
  • I just updated my answer – Frank nike Jan 28 '23 at 13:11