EngineSense
EngineSense

Reputation: 3616

class with argument and variable in Dart save to Firebase at wrong fields

I'm into Dart/Flutter development, I'm slowly catching few things that's really weird.

Let's say

class Person{
 final String name;
 final String age;
 final String address;
    Person({this.name, this.address,this.age});
}

Then I tried to use this class and pass arguments onto the class.

person = Person(name:"killedVariableSwitch", age:"1024", address:"Mars");

So I use the above class to save it in someother database or any class that requires the above implementation. Drastically fails to order itself. My previous code with other programs corrects itself as I remember.

You can notice that, I switched age and address position in that argument. Now the database holds the key:value pair is wrong.

print(person.fromDatabase);

name: killedVariableSwitch, address:1024, age:Mars.

Is this desired output or it should correct itself?. I never experienced this previous android or java. Or I'm doing something wrong here.

More example shot from my doing.

enter image description here

After saving in Firebase Cloudfirestore

enter image description here

Thanks.

Upvotes: 0

Views: 102

Answers (1)

EngineSense
EngineSense

Reputation: 3616

Yes, Indeed that's the cause, If I place the order different in main class, Then the passing arguments have real side effects!.

Dart need to fix this I guess. Or I'm doing totally wrong!!!

@JsonSerializable()
class User extends Equatable {
 final String id;
 final String name;
 final String mobileNo;
 final String email;
 final String picture;
 final String dateCreated;
 final String fcmTokenId;

const User(
  {this.id,
  this.name,
  this.mobileNo,
  this.email,
  this.picture,
  this.dateCreated,
  this.fcmTokenId});

The onpress call

context.read<HomeBloc>().add(AddUser(User(
            name: "NoName",
            dateCreated:DateTime.now().toString(),
            email: "s.puspahraj.com",
            fcmTokenId: "dddadasda",
            picture: "ddsf",
            mobileNo:  _user.mobileNo)))



 @JsonSerializable()
 class UserEntity extends Equatable {
  final String id;
  final String name;
  final String mobileNo;
  final String email;
  final String picture;
  final String dateCreated;
  final String fcmTokenId;

Mixed up here!!!!

 Map<String, Object> toDocument() {
  return {
  "id": id,
  "name": name,
  "dateCreated": dateCreated,
  "email": email,
  "picture": picture,
  "mobileNo": mobileNo,
  "fcmTokenId": fcmTokenId
   };
  ***************************** 
 @override
  Future<void> addUser(User user) {
   return usersCollection
    .add(user.toEntity().toDocument())
    .then((value) => _logger.d('user added'))
    .catchError((error) => _logger.e("Failed to add user: $error"));

So I have to be really careful while placing those arguments!

Upvotes: 1

Related Questions