Need help in resolving an exception saying "Bad state: Tried to read a provider that threw during the creation of its value."
The Error!
The following _CastError was thrown building Consumer<CartItemCounter>(dirty, dependencies:
[_InheritedProviderScope<CartItemCounter?>]):
Null check operator used on a null value
The relevant error-causing widget was:
Consumer<CartItemCounter>
Consumer:file:///C:/Users/USER/OneDrive/WebProjects/FLUTTER/PRACTICE/UDEMY/Build_eCommerce_App/e_shop/lib/Sto re/storeHome.dart:69:30
Model File: cartItemCounter.dart
#Am I doing the wrong implementation of the null operator here in this file?
import 'package:e_shop/Config/config.dart';
import 'package:flutter/foundation.dart';
class CartItemCounter extends ChangeNotifier {
int _counter = EcommerceApp.sharedPreferences
!.getStringList(EcommerceApp.userCartList)!
.length -
1;
int get count => _counter;
Future<void> displayResult() async {
int _counter = EcommerceApp.sharedPreferences!
.getStringList(EcommerceApp.userCartList)!
.length -
1;
await Future.delayed(
Duration(milliseconds: 100),
() {
notifyListeners();
},
);
}
}
#Implementation: Consuming the Model on my home page is as below.
Positioned(
child: Stack(
children: [
Icon(
Icons.ac_unit,
size: 20.0,
color: Colors.greenAccent,
),
Positioned(
top: 3.0,
bottom: 4.0,
left: 4.0,
child: Consumer<CartItemCounter>(
builder: (context, counter, child) => Text(
EcommerceApp.sharedPreferences!
.getStringList(EcommerceApp.userCartList)!
.length -
1 as String,
//'${EcommerceApp.sharedPreferences!.getStringList
// (EcommerceApp.userCartList)!.length - 1}',
style: TextStyle(
color: Colors.white,
fontSize: 12.0,
fontWeight: FontWeight.w500),
),
),
)