hermie_brown
hermie_brown

Reputation: 329

Issue with reading UserData from Firebase

The picture below shows how my DB is structured. Every model class is a top level collection.

how my firebase is structured

In my userData collection, I save additional information about users when they first sign up such as name, address, phone number, etc. I can successfully write to firebase but I'm having problem reading from firebase to my form fields.

I've pasted my UserData class model and Notifier class below so you can see what I've done.

What could I be missing please in my getUserData()? Or am I doing it all wrong? Can someone show me a better way to achieve reading current logged in userData from Firebase?

class User {
 final String uid;
 final String email;
 final String password;
 User({this.uid, this.email, this.password});
}

class UserData {
  String id;
  String firstName;
  String lastName;
  String phoneNumber;
  String role;
  String businessName;
  String businessType;
  String streetAddress;
  String city;
  String state;
  String postcode;
  String country;
  Timestamp createdAt;
  Timestamp updatedAt;
  UserData(
    this.id,
    this.firstName,
    this.businessType,
    this.businessName,
    this.city,
    this.country,
    this.createdAt,
    this.lastName,
    this.phoneNumber,
    this.postcode,
    this.role,
    this.state,
    this.streetAddress,
    this.updatedAt,
  );
  UserData.fromMap(Map<String, dynamic> data) {
    id = data['id'];
    firstName = data['first_name'];
    lastName = data['last_name'];
    phoneNumber = data['phone_number'];
    role = data['role'];
    businessName = data['business_name'];
    businessType = data['business_type'];
    streetAddress = data['street_address'];
    city = data['city'];
    postcode = data['postcode'];
    state = data['state'];
    country = data['country'];
    createdAt = data['created_at'];
    updatedAt = data['updated_at'];
  }
  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'first_name': firstName,
      'last_name': lastName,
      'phone_number': phoneNumber,
      'role': role,
      'business_name': businessName,
      'business_type': businessType,
      'street_address': streetAddress,
      'city': city,
      'postcode': postcode,
      'state': state,
      'country': country,
      'created_at': createdAt,
      'updated_at': updatedAt,
    };
  }
}

//UserDataNotifier class

class UserDataNotifier with ChangeNotifier {

  UserData _currentLoggedInUserData;

  CollectionReference userDataRef = Firestore.instance.collection('userData');

  UserData get currentLoggedInUserData => _currentLoggedInUserData;

  Future<UserData> getUserData() async {
    String userId = (await FirebaseAuth.instance.currentUser()).uid;
    DocumentSnapshot variable =
        await Firestore.instance.collection('userData').document(userId).get();
    _currentLoggedInUserData = variable.data as UserData;
    notifyListeners();
  }

  Future createOrUpdateUserData(UserData userData, bool isUpdating) async {
    String userId = (await FirebaseAuth.instance.currentUser()).uid;
    if (isUpdating) {
      userData.updatedAt = Timestamp.now();
      await userDataRef.document(userId).updateData(userData.toMap());
      print('updated userdata with id: ${userData.id}');
    } else {
      userData.createdAt = Timestamp.now();
      DocumentReference documentReference = userDataRef.document(userId);
      userData.id = documentReference.documentID;
      await documentReference.setData(userData.toMap(), merge: true);
      print('created userdata successfully with id: ${userData.id}');
    }
    notifyListeners();
  }
}

Upvotes: 0

Views: 60

Answers (1)

Rajitha Udayanga
Rajitha Udayanga

Reputation: 1732

_currentLoggedInUserData = variable.data() as UserData; 

call variable.data as variable.data()

if not working,

UserData.fromMap(Map<String, dynamic> data) => UserData(
    id: data['id'];
    firstName: data['first_name'];
    lastName: data['last_name'];
    phoneNumber: data['phone_number'];
    role: data['role'];
    businessName: data['business_name'];
    businessType: data['business_type'];
    streetAddress: data['street_address'];
    city: data['city'];
    postcode: data['postcode'];
    state: data['state'];
    country: data['country'];
    createdAt: data['created_at'];
    updatedAt: data['updated_at'];
  })

_currentLoggedInUserData = UserData.fromMap(variable.data());

Upvotes: 2

Related Questions