Search code examples
flutterflutter-getxtext-widgetflutter-sharedpreference

The argument type 'String?' can't be assigned to the parameter type 'String' because 'String?' is nullable and 'String' isn't in Text widget


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


Solution

  • 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.