Provider giving null value

Viewed 149

I'm trying to provide name of the user to another screen when after user logs in using email password. From the API I get the name of the user as a response so I want to use that name in next screen so I'm using provider to access that name but I'm getting null.

How I calling the API and assigning the user,

class LoginStore = _LoginStore with _$LoginStore;

abstract class _LoginStore with Store {
  @observable
  NetworkStatus networkStatus = NetworkStatus.idle;

  User user;   //<<<<<<<<<<<< User Model

  Future<void> validateForm() async {
    networkStatus = NetworkStatus.loading;
    var connectivity = await NetworkUtils().checkIsInternet();

    if (formKey.currentState.validate()) {
      if (!connectivity) {
        networkStatus = NetworkStatus.error;
        showToast(AppStrings.noInternet, ApiResult.unsuccessful);
      } else {
        final result = await Repository().loginApiCall(   //<<<<< API call
          LoginRequest(
            email: emailController.text,
            password: passwordController.text,
          ),
        );

        if (result.success) {
          user = result.data;  //<<<<<<< assign the Data
 
          SharedPreferencesHelper.setParentData(something);
          showToast(AppStrings.loginSuccessful, ApiResult.successful);
          SharedPreferencesHelper.setLoginStatus(true);
          networkStatus = NetworkStatus.success;
        } else {
          networkStatus = NetworkStatus.error;
          showToast(result.message, ApiResult.unsuccessful);
        }
      }
    } else {
      networkStatus = NetworkStatus.idle;
    }
  }
  }
}

How I am accessing it,

class ChatStreamTab extends StatelessWidget {
  final HomeScreenTabStore tabStore = HomeScreenTabStore();

  @override
  Widget build(BuildContext context) {
    final headline6 = Theme.of(context).textTheme.headline6;
    final user = Provider.of<LoginStore>(context);  //<<<<< provider
    return Scaffold(
      backgroundColor: Colors.transparent,
      extendBody: true,
      body: GradientBackground(
        child: SingleChildScrollView(
          child: SafeArea(
            child: Column(
              children: [
                InkWell(
                  onTap: () => logoutUser(context),
                  child: Padding(
                    padding: EdgeInsets.only(
                      right: 20.sdpWidth,
                      top: 10.sdpHeight,
                    ),
                    child: Align(
                      alignment: Alignment.topRight,
                      child: Text(
                        AppStrings.logout,
                        style: headline6.copyWith(
                          fontSize: 16.sdpHeight,
                          color: Colors.white,
                        ),
                      ),
                    ),
                  ),
                ), 
                Text(
                  user.user.name,  //<<<<<< accessing the here
                  style: headline6.copyWith(
                    color: AppColors.poloBlue,
                    fontWeight: FontWeight.w600,
                    fontSize: 24.sdpHeight,
                  ),
                ),
             );
          }
      }

User model,

@JsonSerializable()
class User {
  String password;
  String id;
  String email;
  String name; //<<<<< name field

  User({
    this.password,
    this.id,
    this.email,
    this.name,
  });

  factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);

  Map<String, dynamic> toJson() => _$UserToJson(this);
}

everything is working fine, if I print it in the API class then I do get the value but I'm not getting the name field using provider I get null.

0 Answers
Related