Home > Blockchain >  The argument type 'String?' can't be assigned to the parameter type 'String'
The argument type 'String?' can't be assigned to the parameter type 'String'

Time:09-25

I am trying to display the name and email of the person when he logs in to the profile screen using Getx

                              Column(
                                children: [
                                  Text(
                                    controller.userModel!.name,
                                    style: TextStyle(
                                      fontSize: 20,
                                      fontWeight: FontWeight.w600,
                                      color: Kprimarycolor,
                                    ),
                                  ),
                                  Text(
                                    controller.userModel!.email,
                                    style: TextStyle(
                                      fontSize: 20,
                                      fontWeight: FontWeight.w600,
                                      color: Kprimarycolor,
                                    ),
                                  ),
                                ],
                              ),
                            ],

but this error keep showing Error in vs code and Error in terminal

the related code to name and email is

class UserModel {
  late String? userId, email, name, pic;

  UserModel({
    required this.userId,
    required this.email,
    required this.name,
    required this.pic,
  });
  UserModel.fromJson(Map<dynamic, dynamic> map) {
    userId = map['userId'];
    email = map['email'];
    name = map['name'];
    pic = map['pic'];
  }
  toJson() {
    return {
      'userId': userId,
      'email': email,
      'name': name,
      'pic': pic,
    };
  }
}

I tried to add .toString() and as String but the error keeps showing after debugging

CodePudding user response:

Column(
                            children: [
                              Text(
                                controller.userModel!.name!,
                                style: TextStyle(
                                  fontSize: 20,
                                  fontWeight: FontWeight.w600,
                                  color: Kprimarycolor,
                                ),
                              ),
                              Text(
                                controller.userModel!.email!,
                                style: TextStyle(
                                  fontSize: 20,
                                  fontWeight: FontWeight.w600,
                                  color: Kprimarycolor,
                                ),
                              ),
                            ],
                          ),
                        ],

I added '!' character, it should work.

CodePudding user response:

In your model, late String? userId, email, name, pic; and @Salih Can answer will be work.

Here, String? means string can accept null value. But Text widget don't accept null value. You need to use bang operator ! to handle it and by adding ! means this value is not null anymore. A better practice would be checking if it is null or not, then assign on Text. It can be

  • Text(myVal==null? "defalut value": myVal)
  • Text(myVal??"default Value")
  • if(myval!=null) Text(myVal) and it will render only if string is not null.
  • Related