skip to Main Content

What is causing the following error at line Text(getGreetingText(user.firstName));

I am printing the value of user.firstName and it is not null
I have also tried to defined the user with the firstName field with ? but I get the same error

The user object is fetched via http and I am using json_serializable to convert it from json to user model

  late Future<User> _user;

  @override
  initState() {
    _user = _getUserData();
    super.initState();
  }


FutureBuilder(
                      future: _user,
                      builder: (BuildContext context,
                          AsyncSnapshot<User> snapshot) {
                        if (snapshot.connectionState == ConnectionState.done) {
                          if (snapshot.hasError) {
                            return Center(
                              child: Text(
                                '${snapshot.error} occurred',
                                style: const TextStyle(fontSize: 18),
                              ),
                            );
                          } else if (snapshot.hasData) {
                            final user = snapshot.data;
                            logger.i(user.firstName);
                            Text(getGreetingText(user.firstName));
                          }
                        }

@JsonSerializable(explicitToJson: true)
class User {
  @JsonKey(name: 'user_id')
  final String userId;
  @JsonKey(name: 'given_name')
  final String firstName;
  @JsonKey(name: 'family_name')
  final String lastName;
  @JsonKey(name: 'email')
  final String email;
  @JsonKey(name: 'preferred_username')
  final String userName;
  @JsonKey(name: 'organization_id')
  final String organization;
  @JsonKey(name: 'phone_number')
  final String phoneNumber;
  @JsonKey(name: 'phone_number_verified')
  final bool phoneNumberVerified;
  @JsonKey(name: 'photos')
  final ProfilePhoto photos;

  User(
      {required this.userId,
      required this.firstName,
      required this.lastName,
      required this.email,
      required this.userName,
      required this.organization,
      required this.photos,
      required this.phoneNumber,
      required this.phoneNumberVerified});

2

Answers


  1. Flutter: type ‘Null’ is not a subtype of type ‘String’.
    What it is telling you is that one of the fields of the object is getting a null value.
    Check with debug when executing the fromJson function, inside the _getUserData function.

    Login or Signup to reply.
  2. I think one of two things may be happening.

    getGreetingText expects to receive a string but receives a null or getGreeting is returning a null when it should return a string.

    So to solve it you can do:

     Text(getGreetingText(user?.firstName ?? "no name yet") ?? "no name yet");
    

    ?? What it does is that if the value is null, it puts a value in this case: "no user yes" or it can also be "loading"

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