Home > front end >  Flutter: The getter 'userId' isn't defined for the class 'Object'
Flutter: The getter 'userId' isn't defined for the class 'Object'

Time:08-25

Edit: Actually I think I should be using the containsKey() method, but Dart doesn’t like that either.

I created a class of CloudGroup which I use to create a stream later. The field that is giving me trouble the groupMembersfield.

The way this field looks in my database is as follows:

groupMembers: { uid1: {...}, uid2: {...}, ... }

I have the type labeled as Object because it is a giant object with children. Why doesn't Dart like this? Thank you in advance. This is the rest of the code.

CloudGroup Class

@immutable
class CloudGroup {
  final String groupAdmin;
  final Object groupMembers;
  final String groupName;

  const CloudGroup({
    required this.groupAdmin,
    required this.groupMembers,
    required this.groupName,
  });

  CloudGroup.fromSnapshot(QueryDocumentSnapshot<Map<String, dynamic>> snapshot)
      : documentId = snapshot.id,
        groupAdmin = snapshot.data()[groupAdminFieldName],
        groupMembers = snapshot.data()[groupMembersFieldName],
        groupName = snapshot.data()[groupNameFieldName],
}

Stream Function

Stream<Iterable<CloudGroup>> allGroups({required String userId}) =>
      groups.snapshots().map((event) => event.docs
          .map((doc) => CloudGroup.fromSnapshot(doc))
          .where((group) => group.groupMembers.userId == userId));

The error occurs above at the .userId spot. I want to access the child keys to match with the userId provided.

Error

Error: The getter 'userId' isn't defined for the class 'Object'.

 - 'Object' is from 'dart:core'.
Try correcting the name to the name of an existing getter, or defining a getter or field named 'userId'.
          .where((group) => group.groupMembers.userId == userId));

If anyone does know the solution but also could explain it so I understand for the future, I would really appreciate it. Thank you again!

CodePudding user response:

In Your CloudGroup class, it has Instance variable named groupMembers

groupMembers's type is Object

This Object type is defined in core/object.dart and has no getter named 'userId'

If you want to use like groupMembers.userId

you have to make groupMembers class like

class GroupMembers {
 final int userId;
 // other instance variable you wants
}

then use

class CloudGroup {
  final String groupAdmin;
  final GroupMembers groupMembers;
  final String groupName;

  const CloudGroup({
    required this.groupAdmin,
    required this.groupMembers,
    required this.groupName,
  });

  CloudGroup.fromSnapshot(QueryDocumentSnapshot<Map<String, dynamic>> snapshot)
      : documentId = snapshot.id,
        groupAdmin = snapshot.data()[groupAdminFieldName],
        groupMembers = snapshot.data()[groupMembersFieldName],
        groupName = snapshot.data()[groupNameFieldName],
}

Here is link about Object class

https://api.flutter.dev/flutter/dart-core/Object-class.html

I hope this answer helps you

  • Related