Reputation: 427
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),
),
),
)
Upvotes: 2
Views: 1387
Reputation: 77304
You used the !
operator a lot. Your compiler is warning you, that in some cases the variables you rely on hold no value, and instead of listening to your compiler and thinking about what you might want your program to do when that is the case, you basically yelled "oh shut the f*** up" and slammed the door by putting in a !
. This !
did not solve a single problem though. The variable might still not hold a value, you only yelled at the one tool trying to help you with it to stop doing so. Your program still has unfixed problems, you just suppressed early reporting and now you got late reporting... aka a crash.
Remove all !
used to suppress warnings. Then look at each warning. Why could that variable be null here?
Why is EcommerceApp.sharedPreferences
nullable? Well, it's your code, you decide. If you want to keep it as nullable, what do you want to do if it's null?
getStringList
returns a list, but it can also return null. For example when you start your app for the first time, there will be no data. How do you want to handle that?
You have to answer those questions, not just tell your compiler to shut up about them. And I cannot answer them for you. It's your program. You decide.
So as an example, instead of this horrible line:
EcommerceApp.sharedPreferences!
.getStringList(EcommerceApp.userCartList)!
.length - 1;
That will fail if either sharedPreferences
is null or getStringList
returns null, you should think and make decisions what should happen if that is the case. I know the code can be more conscise, but this is a learning exercise, so we will do it the verbose way:
int UserCartListCount() {
final source = EcommerceApp.sharedPreferences;
if(source == null) {
// we have no way to read our data, so what now?
return 0;
}
final cartList = source.getStringList(EcommerceApp.userCartList);
if(cartList == null) {
// there was no cart previously saved
return 0;
}
// this is weird... but it's your logic, I have no idea why that would be correct
return cartList.length - 1;
}
And then, you call this function instead in your code.
Upvotes: 3