iam using cubit for state managment and hive for local DB
the app is ecommerce the problem type 'String' is not a subtype of type 'int' in type cast
occures when user enter the store page when the initState
func is called in it a method is called
via context.read<CounterCubit>().initCounter(...);
note: everything is fine with datatypes in initialization for the properties of the initCounter and the variables inside the func itself
note: the main func, future func , cart model and the adapter dont have a close relation to the problem I think but I provided it for the bigger picture u know
the initCounter func :
void initCounter(int storeId, String storeName, String storeNameperId) {
final cartBox = Hive.box<CartHive>('cartBox');
if (cartBox.containsKey(storeId)) {
final cartData = cartBox.get(storeId)!;
final storeNameperCounterperProduct = cartData.storeNameCounterperProduct;
final storeNameperPriceperProduct = cartData.storeNamePriceperProduct;
final prodsCountPerStore = cartData.prodsCountPerStore;
final updatedStoreNameperCounterperProduct =
Map<String, int>.from(storeNameperCounterperProduct);
final updatedStoreNameperPriceperProduct =
Map<String, String>.from(storeNameperPriceperProduct);
final updatedProdsCountPerStore =
Map<String, int>.from(prodsCountPerStore);
emit(
CounterInItState(
storeNamePriceperProduct: updatedStoreNameperPriceperProduct,
storeNameCounterperProduct: updatedStoreNameperCounterperProduct,
prodsCountPerStore: updatedProdsCountPerStore,
),
);
} else {
emit(
CounterInitialState(
storeId: storeId,
storeName: storeName,
prodsCountPerStore: {},
storeNamePriceperProduct: {},
storeNameCounterperProduct: {},
),
);
}
}
counterstate:
class CounterState extends Equatable {
Map<String, String> storeNamePriceperProduct;
Map<String, int> storeNameCounterperProduct;
Map<String, int> prodsCountPerStore;
int? storeId;
String? storeName;
CounterState({
this.storeId,
this.storeName,
required this.storeNamePriceperProduct,
required this.storeNameCounterperProduct,
required this.prodsCountPerStore,
});
@override
List<Object?> get props => [
storeId,
storeName,
storeNamePriceperProduct,
storeNameCounterperProduct,
prodsCountPerStore,
];
}
class CounterInItState extends CounterState {
CounterInItState({
super.storeId,
super.storeName,
required super.storeNamePriceperProduct,
required super.storeNameCounterperProduct,
required super.prodsCountPerStore,
});
@override
List<Object?> get props => [...super.props];
}
the main func + Future func :
Box? cartBoxG;
Future<Box> openHiveCartBox() async {
if (!Hive.isBoxOpen('cartBox')) {
Hive.init((await getApplicationDocumentsDirectory()).path);
}
return await Hive.openBox<CartHive>('cartBox');
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
Hive.registerAdapter(FavouriteStoreAdapter());
Hive.registerAdapter(CartAdapter());
cartBoxG = await openHiveCartBox();
runApp(MyApp());
}
carthive (cart model) :
@HiveType(typeId: 1)
class CartHive {
@HiveField(0)
Map<String, String> storeNamePriceperProduct;
@HiveField(1)
Map<String, int> storeNameCounterperProduct;
@HiveField(2)
Map<String, int> prodsCountPerStore;
CartHive(
this.storeNameCounterperProduct,
this.storeNamePriceperProduct,
this.prodsCountPerStore,
);
}
the adapter :
class CartAdapter extends TypeAdapter<CartHive> {
@override
CartHive read(BinaryReader reader) {
return CartHive(
(reader.readMap()).cast<String, int>(),
(reader.readMap()).cast<String, String>(),
(reader.readMap()).cast<String, int>(),
);
}
@override
int get typeId => 1;
@override
void write(BinaryWriter writer, CartHive obj) {
writer
..writeMap(obj.storeNamePriceperProduct)
..writeMap(obj.storeNameCounterperProduct)
..writeMap(obj.prodsCountPerStore);
}
}
note : there is multiple func besides initCounter
some to store/update data and so on
the first 10 frames from the stacktrace :
[log] initcounter #0 CastMap.forEach.<anonymous closure> (dart:_internal/cast.dart:290:25)
cast.dart:290
#1 _LinkedHashMapMixin.forEach (dart:_compact_hash:764:13)
#2 CastMap.forEach (dart:_internal/cast.dart:289:13)
#3 new LinkedHashMap.from(dart:collection/linked_hash_map.dart:192:11)
#4 CounterCubit.initCounter(package:easyshop/cubit/counter/counter_cubit.dart:44:54)
#5 _StoreProfileState.initState(package:easyshop/store/storeprofile/store_profile.dart:59:34)
#6 StatefulElement._firstBuild(package:flutter/src/widgets/framework.dart:5842:55)
#7 ComponentElement.mount(package:flutter/src/widgets/framework.dart:5691:5)
#8 Element.inflateWidget(package:flutter/src/widgets/framework.dart:4539:16)
#9 Element.updateChild(package:flutter/src/widgets/framework.dart:4004:18)
#10 SingleChildRenderObjectElement.mount(package:flutter/src/widgets/framework.dart:7008:14)
note : i found out what causes the problem the problem is that the variable
storeNameperCounterperProduct
is being stored/updated with the data in storeNameperPriceperProduct
and the latter do the same too (e.g. storeNameperCounterperProduct
of type Map<String, int>
is stored as if of type Map<String, String>
and the other do the same) currently iam working on figuring out where this happens but so far nothing seems wrong in the code everything is in its place which is weird
I think the problem here is that you have your adapter read method in the wrong order. Can you confirm if the following fixes it?
CartHive read(BinaryReader reader) {
return CartHive(
(reader.readMap()).cast<String, String>(), // the first arg has String values
(reader.readMap()).cast<String, int>(), // the second has int values
(reader.readMap()).cast<String, int>(),
);