'retrieving data from hive gives type cast error in flutter
I am trying to get the weight data from hive using flutter, however, i am getting a below error:
type 'double' is not a subtype of type 'UserDataModel'
My user data is :
@HiveType(typeId: 2)
class UserDataModel extends HiveObject {
@HiveField(0)
final String lifestyle;
@HiveField(1)
final String sex;
@HiveField(2)
final String photoData;
@HiveField(3)
final int age;
@HiveField(4)
final int height;
@HiveField(5)
final double weight; plus consturctor & adapter.
I am trying to display an amount by calling a userBox:
body: SafeArea(
child: ListView.builder(
itemCount: hiveBox.length,
itemBuilder: (BuildContext context, int index) {
ExerciseBox data = hiveBox.getAt(index);
UserDataModel datas = userBox.getAt(index);
return Container(
child: InkWell(
onTap: () {
},
),
CupertinoActionSheetAction(
child:
Text(Languages.of(context)!.deleteWorkout),
onPressed: () {
setState(() {
hiveBox.deleteAt(index);
});
Navigator.pop(context);
},
)
],
),
);
} else {
showModalBottomSheet(
context: context,
builder: (context) {
return Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ListTile(
title: Text(
data.date.year.toString() +
"/" +
data.date.month.toString() +
'/' +
data.date.day.toString() +
' ' +
data.date.hour.toString() +
":" +
data.date.minute.toString(),
style: const TextStyle(
color: Colors.black,
fontSize: 15,
fontWeight: FontWeight.w500),
),
),
ListTile(
//leading: new Icon(Icons.photo),
title:
Text(Languages.of(context)!.useThisSettings),
onTap: () {
if (hiveBox.getAt(index).isCustom == true)
{
ScaffoldMessenger.of(context)
..removeCurrentSnackBar()
..showSnackBar(SnackBar(
content:
Text(Languages.of(context)!.settingsNoUse)));
} else {
setState(() {
int duration = data.exerciseTime *
data.numberExercise *
data.numberRepetitions +
data.restTime *
data.numberRepetitions *
(data.numberExercise - 1) +
data.changeRepTime *
(data.numberRepetitions - 1);
MinutesTotal = (duration ~/ 60)
.toString()
.padLeft(2, '0');
SecondsTotal = (duration % 60)
.toString()
.padLeft(2, '0');
BurnedCalories =
(datas.weight*4.5*0.0175*int.parse(MinutesTotal))
.toString().padLeft(2, '0');
hiveBox.put('burnedCalories',
int.parse(BurnedCalories));
});
}
Navigator.pop(context);
},
),
);
},
);
}
},
how to retrieve the data from the box? As u can see I am using 2 different box data, that should not be a problem, right? I am just learning flutter and very new to hive. help appreciated very much! Thanks!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
