Home > front end >  Firebase Flutter Exception - type 'String' is not a subtype of type 'int' of �
Firebase Flutter Exception - type 'String' is not a subtype of type 'int' of �

Time:07-22

I'm trying to fetch data from firebase. in my collection, there is child 2129. The 2129 has one more child 911. I'm getting json response. But I'm not able to parse using model class. It shows an exception type 'String' is not a subtype of type 'int' of 'index. Here is what I'm using -

Firebase Data:

2129 -> 911 ->
TimeStamp:1657782900738
connectedStatus:"Requested"
imagePath:
"https://www.ppp.in/placeholder.png"
lastMessage:""
status:"Active"
time:"2022-07-14 12:45:00"
typing:false

{
"imagePath":"https://asd-123.jpg",
"lastMessage":"hi",
"typing":false,
"connectedStatus":"Pending",
"unreadMessageCount":0,
"time":"2022-07-21 16:22:58",
"userId":2277,
"TimeStamp":1658400778129,
"status":"Unactive",
"username":"Users Name"
}

This is the response I'm getting from Firebase RLTD. I'm trying to parse it using model class UserModel as Follows:

UserDataModel mUserIdFromJson(String str) => UserDataModel.fromJson(json.decode(str));
String mUserIdToJson(UserDataModel data) => json.encode(data.toJson());
class UserDataModel {
  UserDataModel({
      String? imagePath, 
      String? lastMessage, 
      bool? typing, 
      String? connectedStatus, 
      int? unreadMessageCount, 
      String? time, 
      int? userId, 
      int? timeStamp, 
      String? status, 
      String? username,}){
    _imagePath = imagePath;
    _lastMessage = lastMessage;
    _typing = typing;
    _connectedStatus = connectedStatus;
    _unreadMessageCount = unreadMessageCount;
    _time = time;
    _userId = userId;
    _timeStamp = timeStamp;
    _status = status;
    _username = username;
}

  UserDataModel.fromJson(dynamic json) {
    _imagePath = json['imagePath'];
    _lastMessage = json['lastMessage'];
    _typing = json['typing'];
    _connectedStatus = json['connectedStatus'];
    _unreadMessageCount = json['unreadMessageCount'];
    _time = json['time'];
    _userId = json['userId'];
    _timeStamp = json['TimeStamp'];
    _status = json['status'];
    _username = json['username'];
  }
  String? _imagePath;
  String? _lastMessage;
  bool? _typing;
  String? _connectedStatus;
  int? _unreadMessageCount;
  String? _time;
  int? _userId;
  int? _timeStamp;
  String? _status;
  String? _username;


  set imagePath(String? value) {
    _imagePath = value;
  }

  String? get imagePath => _imagePath;
  String? get lastMessage => _lastMessage;
  bool? get typing => _typing;
  String? get connectedStatus => _connectedStatus;
  int? get unreadMessageCount => _unreadMessageCount;
  String? get time => _time;
  int? get userId => _userId;
  int? get timeStamp => _timeStamp;
  String? get status => _status;
  String? get username => _username;

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['imagePath'] = _imagePath;
    map['lastMessage'] = _lastMessage;
    map['typing'] = _typing;
    map['connectedStatus'] = _connectedStatus;
    map['unreadMessageCount'] = _unreadMessageCount;
    map['time'] = _time;
    map['userId'] = _userId;
    map['TimeStamp'] = _timeStamp;
    map['status'] = _status;
    map['username'] = _username;
    return map;
  }

  set lastMessage(String? value) {
    _lastMessage = value;
  }

  set typing(bool? value) {
    _typing = value;
  }

  set connectedStatus(String? value) {
    _connectedStatus = value;
  }

  set unreadMessageCount(int? value) {
    _unreadMessageCount = value;
  }

  set time(String? value) {
    _time = value;
  }

  set userId(int? value) {
    _userId = value;
  }

  set timeStamp(int? value) {
    _timeStamp = value;
  }

  set status(String? value) {
    _status = value;
  }

  set username(String? value) {
    _username = value;
  }

And my flutter code is:

void getInfo(int? mOtherUserId) async {
    mUserId = await SecureStorageRepo().getUserProfileId();
    debugPrint('mUser info: $mUserId');
    debugPrint('mOtherUserId info: $mOtherUserId');

    final DatabaseReference mRef = fDatabase
        .ref(ChatDatabase.connectedUsers)
        .child('$mUserId')
        .child('$mOtherUserId');

    mRef.once().then((snapshot) {
      try {
        var userData = snapshot.snapshot.value;
        debugPrint('User Snapshot: $userData');

        var userDataObject = json.encode(userData);
        debugPrint('User Snapshot2: $userDataObject');
        
        // BELOW LINES AREN'T WORKING
        UserDataModel result = UserDataModel.fromJson(userDataObject);
        debugPrint('Other Users result: ${result.toJson()}');
        
      } catch (e) {
        debugPrint('User Data Error: ${e.toString()}');
      }
    });
  }

After this method call, I'm getting an exception from catch block as -

**User Data Error: type 'String' is not a subtype of type 'int' of 'index'**

How can I resolve this? What and Where is exactly the issue?

CodePudding user response:

you encoded json in place of decode I think

var userDataObject = json.encode(userData);
        debugPrint('User Snapshot2: $userDataObject');

This has to be

var userDataObject = json.decode(userData);
        debugPrint('User Snapshot2: $userDataObject');

EDIT

Use this model

class UserDataModel {
  String? imagePath;
  String? lastMessage;
  bool? typing;
  String? connectedStatus;
  int? unreadMessageCount;
  String? time;
  int? userId;
  int? timeStamp;
  String? status;
  String? username;

  UserDataModel(
      {this.imagePath,
      this.lastMessage,
      this.typing,
      this.connectedStatus,
      this.unreadMessageCount,
      this.time,
      this.userId,
      this.timeStamp,
      this.status,
      this.username});

  UserDataModel.fromJson(Map<String, dynamic> json) {
    imagePath = json['imagePath'];
    lastMessage = json['lastMessage'];
    typing = json['typing'];
    connectedStatus = json['connectedStatus'];
    unreadMessageCount = json['unreadMessageCount'];
    time = json['time'];
    userId = json['userId'];
    timeStamp = json['TimeStamp'];
    status = json['status'];
    username = json['username'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['imagePath'] = this.imagePath;
    data['lastMessage'] = this.lastMessage;
    data['typing'] = this.typing;
    data['connectedStatus'] = this.connectedStatus;
    data['unreadMessageCount'] = this.unreadMessageCount;
    data['time'] = this.time;
    data['userId'] = this.userId;
    data['TimeStamp'] = this.timeStamp;
    data['status'] = this.status;
    data['username'] = this.username;
    return data;
  }
}

From response you can do the following

var convertedJson = json.decode(response.toString);
UserDataModel userData = UserDataModel.fromJson(convertedJson);

Now userData will have all the data.

  • Related