Yashwardhan Pauranik
Yashwardhan Pauranik

Reputation: 5566

How to loop over a Map and update the property in the existing model in Flutter

I have a class SocialAuth, which holds some common properties of the AuthModel class. Now I want to dynamically update the instance of AuthModel based on the object of SocialAuth class.

I'm looping over the .toJson() of SocialAuth and trying to update the property of _authModel (Instance of AuthModel) dynamically.

ERROR IS - The operator '[]=' isn't defined for the type 'AuthModel'.

SocialAuth Class

@JsonSerializable()
class SocialAuth {
  String? email;
  String? firstName;
  String? lastName;
  String? photoURL;

  SocialAuth({
    this.email,
    this.firstName,
    this.lastName,
    this.photoURL,
  });

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

  @override
  String toString() {
    return 'SocialAuth{email: $email, firstName: $firstName, lastName: $lastName, photoURL: $photoURL}';
  }
}

In my widget class, I'm calling a function updateAuthModel() which resides in my provider class like below:

 // Called from StateFull Widget class
  void _invokeGoogleAuth() async {
    var user = await SocialAuthService().getGoogleAuthData();
    _notifier.updateAuthModel(user!.toJson());
  }
// Provider class
...
void updateAuthModel(Map<String, dynamic> user) {
  user.forEach((key, value) {
   _authModel['$key'] = value; // IDE Error - The operator '[]=' isn't defined for the type 'AuthModel'.
  });
}
...

AuthModel

import 'package:aphrodite_v2/data/enums/enums.dart';

class AuthModel {
  String? firstName;
  String? lastName;
  String? mobile;
  String? email;
  String? dob;
  String? otp;
  String? pin;
  AuthStatus status;

  AuthModel({
    this.firstName,
    this.lastName,
    this.mobile,
    this.dob,
    this.email,
    this.status = AuthStatus.UNDEFINED,
    this.otp,
    this.pin,
  });

  Map<String, dynamic> toMap() {
    return {
      'firstName': firstName,
      'lastName': lastName,
      'mobile': mobile,
      'dob': dob,
      'email': email,
      'status': status,
      'otp': otp,
      'pin': pin,
    };
  }
}

Upvotes: 0

Views: 1028

Answers (1)

Rohan Thacker
Rohan Thacker

Reputation: 6357

You get the error, as the operator []= isn't defined for the type AuthModel as the AuthModel class has not defined the [] operator.

You will need to define the operator [] in the AuthModel class,

void operator []=(String key, dynamic value) {
// Do this for each field in your class
if (key == 'firstName') firstName = value;
if (key == 'lastName') lastName = value;
...
}

If you want your class to behave like a Map object you can also mixin the MapMixin.

import 'dart:collection';

class AuthModel with MapMixin {
  @override
  operator [](Object? key) {
    // TODO: implement []
    throw UnimplementedError();
  }

  @override
  void operator []=(key, value) {
    // TODO: implement []=
  }

  @override
  void clear() {
    // TODO: implement clear
  }

  @override
  // TODO: implement keys
  Iterable get keys => throw UnimplementedError();

  @override
  remove(Object? key) {
    // TODO: implement remove
    throw UnimplementedError();
  }
}

Docs for MapMixin class

Upvotes: 3

Related Questions