I'm using supabase as my backend and sqflite as my local DB. I'm updating the user info in supabase successfully and inerting them in local db the right way. but when I try get the user info that is stored in the local db and display it on the screen i get this error:
type 'int' is not a subtype of type 'String' in type cast
I have looked everywhere in the code but i cant be able to find the mistake i made.
Heres my code
final String? id;
final String? username;
final String? email;
final int? weight;
final int? height;
final String? gender;
final int? age;
const UserModel({
this.id,
this.username,
this.email,
this.weight,
this.height,
this.gender,
this.age,
});
factory UserModel.fromMap(Map<String, dynamic> json) {
return UserModel(
id: json['id'] as String,
username: json['username'] as String?,
email: json['email'] as String?,
weight: json['weight'] as int?,
height: json['height'] as int?,
age: json['age'] as int?,
gender: json['gender'] as String?,
);
}
Map<String, dynamic> toMap() => <String, dynamic>{
'id': id,
'username': username,
'email': email,
'weight': weight,
'height': height,
'gender': gender,
'age': age,
};
}
Heres my local db logic with sqflite:
static addUser(UserModel userModel) async {
final db = await getDB();
var raw = await db.insert('User', userModel.toMap(),
conflictAlgorithm: ConflictAlgorithm.replace);
print(raw);
return raw;
}
Future<UserModel?> getDatabaseModelWithId() async {
try {
final db = await getDB();
var response = await db.query(
'User',
where: 'id = 1',
);
print(response);
if (response.isNotEmpty) {
final userModel = UserModel.fromMap(response.first);
print('userModel: $userModel');
return userModel;
} else {
print('No data found for id = 1');
return null;
}
} catch (e) {
print('Error in getDatabaseModelWithId: $e');
return null;
}
} ```
The logic for adding the user to sqflite DB:
/// Called when user taps `Update` button
Future<void> _updateProfile() async {
final user = supabase.auth.currentUser;
final UserModel model = UserModel(
username: user!.userMetadata!["username"],
email: user.email,
gender: dropdownValue,
age: int.parse(
ageTextEditingController.text.trim(),
),
height: int.parse(
hightTextEditingController.text.trim(),
),
weight: int.parse(
weightTextEditingController.text.trim(),
));
final updates = {
'username': user!.userMetadata!["username"],
'email': user.email,
'gender': dropdownValue,
'age': int.parse(
ageTextEditingController.text.trim(),
),
'height': int.parse(
hightTextEditingController.text.trim(),
),
'weight': int.parse(
weightTextEditingController.text.trim(),
),
};
try {
await supabase
.from('user')
.update(updates)
.eq("id", supabase.auth.currentUser!.id);
await DatabaseHelper.addUser(model);
if (mounted) {
Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(builder: (context) => const Dashboard()),
(Route<dynamic> route) => false);
}
} on PostgrestException catch (error) {
print(error);
} catch (error) {
print(error);
} finally {
if (mounted) {}
}
}
Heres my dashboard where i need to display the username:
FutureBuilder(
future: databaseHelper?.getDatabaseModelWithId(),
builder: (context, AsyncSnapshot<UserModel?> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
print("hi");
if (snapshot.hasError) {
print("Error: ${snapshot.error}");
return Text("Error: ${snapshot.error}");
} else if (snapshot.hasData) {
final user = snapshot.data!;
return Row(
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
user.username ?? 'Default Username',
style: GoogleFonts.poppins(
textStyle: const TextStyle(
color: Colors.white,
letterSpacing: .5,
fontSize: 32),
),
),
const SizedBox(
height: 5,
),
Text(
now,
style: GoogleFonts.poppins(
textStyle: const TextStyle(
color: Colors.white38,
letterSpacing: .5,
fontSize: 15),
),
),
],
)
],
);
} else {
return Text("Error");
}
} else {
return CircularProgressIndicator(); // Show loading indicator
}
},
),
Any idea where the mistake is?
One of your parameters in your db is int but you are parsing it as String, probably id could be that parameter, just cross check again. Also if this is not the case include your response in the question.