Skip to content

Casting current user to custom object #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
pastordee opened this issue Mar 22, 2019 · 13 comments
Closed

Casting current user to custom object #137

pastordee opened this issue Mar 22, 2019 · 13 comments

Comments

@pastordee
Copy link
Contributor

v1.0.16
can anyone help trying to cast current user to custom user object like this
User user = ParseUser.currentUser() as User;

but I keep getting this error type 'Future' is not a subtype of type 'User' in type cast,

Please I'm new to Dart

Thanks

@phillwiggins
Copy link
Member

Hey

Simply add '''await'''' keyword before ''''ParseUser.currentUser()'''.

No need to use the '''as User'''

@pastordee
Copy link
Contributor Author

Hey

Thank you for your quick reply
I did as you suggested but I still get the same message

flutter: type 'ParseUser' is not a subtype of type 'User'

please here is my User object

class User extends ParseUser implements ParseCloneable {
User() : super(keyVarUsername, keyVarPassword, keyVarEmail);
User.clone() : this();

/// Looks strangely hacky but due to Flutter not using reflection, we have to
/// mimic a clone
@OverRide
clone(Map map) => User.clone()..fromJson(map);

static const String keyStatus = 'status';
static const String keyAbout = 'about';
static const String keyDob = 'dob';
static const String keyOnline= 'online';
static const String keyEmailVerified = 'emailVerified';
static const String keyFirstName = 'firstName';
static const String keyLastName = 'lastName';
static const String keyFullName = 'fullName';
static const String keyGender = 'gender';
static const String keyLastSeen = 'lastseen';

static const String keyOrganization = 'organization';
static const String keyBuddies = 'buddies';
static const String keyGroups = 'groups';
static const String keyWarriors = 'warriors';

String get status => get(keyStatus);
set status(String status) => set(keyStatus, status);

String get about => get(keyAbout);
set about(String about) => super.set(keyAbout, about);

DateTime get dob => get(keyDob);
set dob(DateTime dob) => set(keyDob, dob);

bool get online => get(keyOnline);
set online(bool online) => set(keyOnline, online);

bool get emailVerified => get(keyEmailVerified);
set emailVerified(bool emailVerified) => set(keyEmailVerified, emailVerified);

String get firstName => get(keyFirstName);
set firstName(String firstName) => set(keyFirstName, firstName);

String get lastName => get(keyLastName);
set lastName(String lastName) => set(keyLastName, lastName);

String get fullName => get(keyFullName);
set fullName(String fullName) => super.set(keyFullName, fullName);

String get gender => get(keyGender);
set gender(String gender) => set(keyGender, gender);

DateTime get lastseen => get(keyLastSeen);
set lastseen(DateTime lastseen) => set(keyLastSeen, lastseen);

List get organization => get<List>(keyOrganization);
set organization(List organization) => set<List>(keyOrganization, organization);
List get buddies => get<List>(keyBuddies);
set buddies(List buddies) => set<List>(keyBuddies, buddies);
List get groups => get<List>(keyGroups);
set groups(List groups) => set<List>(keyGroups, groups);
List get warriors => get<List>(keyWarriors);
set warriors(List warriors) => set<List>(keyWarriors, warriors);

}

@phillwiggins
Copy link
Member

phillwiggins commented Mar 23, 2019 via email

@phillwiggins
Copy link
Member

Any luck on this one?

@pastordee
Copy link
Contributor Author

Any luck on this one?

sorry no joy here,

var userCurr = await ParseUser.currentUser();

var queryBuilder = QueryBuilder<User>(User())
  ..whereEqualTo("objectId", userCurr.objectId)
  ..includeObject(["creator"]);

final userRe = await queryBuilder.query();
if (userRe.success){
  for (var testObject in userRe.result) {
    globals.user = testObject;
  }
}

this is the work around I'm using for now
using the current user id to query the database and save it to a global user or singleton

@pastordee
Copy link
Contributor Author

User user = _widget.prayers[position].user; not working

User user = _widget.prayers[position].user as ParseUser; not working as well

I still get this: 'ParseUser' is not a subtype of type 'User'

@phillwiggins
Copy link
Member

phillwiggins commented Apr 3, 2019 via email

@pastordee
Copy link
Contributor Author

pastordee commented Apr 3, 2019 via email

@phillwiggins
Copy link
Member

Okay, it's not ideal but there is a fix on the latest branch release/1.0.17.

final User currentUser = await ParseUser.currentUser(customUserObject: User.clone());

Dart is struggling to recognize the saved custom object so you need to pass an instance of the customer object to the SDK.

@pastordee
Copy link
Contributor Author

Thanks will try it, please also i can’t get the array from the server

@gilbriatore
Copy link

Thanks, @phillwiggins, this works for me:

final User currentUser = await ParseUser.currentUser(customUserObject: User.clone());

Okay, it's not ideal but there is a fix on the latest branch release/1.0.17.

final User currentUser = await ParseUser.currentUser(customUserObject: User.clone());

Dart is struggling to recognize the saved custom object so you need to pass an instance of the customer object to the SDK.

@gilbriatore
Copy link

gilbriatore commented May 4, 2022

Thanks, @phillwiggins, this works for me:

final User currentUser = await ParseUser.currentUser(customUserObject: User.clone());

Okay, it's not ideal but there is a fix on the latest branch release/1.0.17.
final User currentUser = await ParseUser.currentUser(customUserObject: User.clone());
Dart is struggling to recognize the saved custom object so you need to pass an instance of the customer object to the SDK.

This strategy can be evolved to:

final User currentUser = await User.currentUser();

by including this static method inside the User class:

static Future<dynamic> currentUser() async {
final String? userJson = await ParseCoreData().getStore().getString(keyParseStoreUser);
if (userJson != null) {
final Map<String, dynamic> userMap = json.decode(userJson);
return User.clone().clone(userMap);
}
return null;
}

@gilbriatore
Copy link

Thanks, @phillwiggins, this works for me:
final User currentUser = await ParseUser.currentUser(customUserObject: User.clone());

Okay, it's not ideal but there is a fix on the latest branch release/1.0.17.
final User currentUser = await ParseUser.currentUser(customUserObject: User.clone());
Dart is struggling to recognize the saved custom object so you need to pass an instance of the customer object to the SDK.

This strategy can be evolved to:

final User currentUser = await User.currentUser();

by including this static method inside the User class:

static Future<dynamic> currentUser() async { final String? userJson = await ParseCoreData().getStore().getString(keyParseStoreUser); if (userJson != null) { final Map<String, dynamic> userMap = json.decode(userJson); return User.clone().clone(userMap); } return null; }

And finally, to get it updated from the server, just include this static method in the User class:

static Future<User> getUpdatedFromServer(String? token) async {
return User.clone().clone(
((await ParseUser(null, null, null, sessionToken: token)
.getUpdatedUser()).result).toJson());
}

and call like this:

final User currentUser = await User.getUpdatedFromServer(currentUser.sessionToken);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants