flutter - 手势捕获的异常处理手势时引发了以下 NoSuchMethodError
问题描述
我是颤振的新手,我正在尝试将产品添加到购物车,但是当我单击添加按钮时,它显示该collection
方法在 null 上调用了我用来将产品添加到用户购物车的函数:
void checkItemInCart(String shortInfoAsID, BuildContext context) {
Constants.sharedPreferences
.getStringList(Constants.userCartList)
.contains(shortInfoAsID)
? Fluttertoast.showToast(msg: "Item is already in Cart.")
: addItemToCart(shortInfoAsID, context);
}
addItemToCart(String shortInfoAsID, BuildContext context) {
List tempCartList =
Constants.sharedPreferences.getStringList(Constants.userCartList);
tempCartList.add(shortInfoAsID);
Constants.firestore
.collection(Constants.collectionUser)
.document(Constants.sharedPreferences.getString(Constants.userUID))
.updateData({
Constants.userCartList: tempCartList,
}).then((v) {
Fluttertoast.showToast(msg: "Item Added to Cart Successfully.");
Constants.sharedPreferences
.setStringList(Constants.userCartList, tempCartList);
Provider.of<CartItemCounter>(context, listen: false).displayResult();
});
}
我也有常量文件,它是:
class Constants {
static const String appName = 'App name';
static SharedPreferences sharedPreferences;
static FirebaseUser user;
static FirebaseAuth auth;
static Firestore firestore;
static String collectionUser = "users";
static String collectionOrders = "orders";
static String userCartList = 'userCart';
static String subCollectionAddress = 'userAddress';
static final String userName = 'name';
static final String userEmail = 'email';
static final String userPhotoUrl = 'photoUrl';
static final String userUID = 'uid';
static final String userAvatarUrl = 'url';
static final String addressID = 'addressID';
static final String totalAmount = 'totalAmount';
static final String productID = 'productIDs';
static final String paymentDetails = 'paymentDetails';
static final String orderTime = 'orderTime';
static final String isSuccess = 'isSuccess';
}
我得到的错误是:
════════ Exception caught by gesture ═══════════════════════════════════════════
The following NoSuchMethodError was thrown while handling a gesture:
The method 'collection' was called on null.
Receiver: null
Tried calling: collection("users")
When the exception was thrown, this was the stack
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1 addItemToCart
package:shipit/Store/storehome.dart:371
#2 checkItemInCart
package:shipit/Store/storehome.dart:362
我希望你能帮助我提前谢谢你感谢它。
解决方案
发生这种情况是Constants.firestore
因为null
. 您需要设置Constants.firestore
为包含Firestore
. 例如,您可以这样做:
// in lib/main.dart
void main() {
WidgetsFlutterBinding.ensureInitialized(); // make sure plugins are initialized
Constants.firestore = Firestore.instance; // Constants.firestore is not null after this line
runApp(MyApp()); // launch the app
}
如果您将main()
方法设置为如下所示,Constants.firestore
当您尝试在小部件/状态管理中使用它时,它将永远不会为空。
您可能还想考虑对其他事情Constants
也这样做(例如FirebaseAuth
,SharedPreferences
等)
推荐阅读
- java - 运行 lint 测试时出现 JAXBException
- karate - 当帖子 'content-type' 是 application/x-www-form-urlencoded 并且 * form field param= {
} - python - 有没有办法在 Azure Databricks 中动态地将参数传递给作业?
- c# - 如何使用 C# 将 `xmlns` 重命名为 TYPE
- python - django-celery 接收任务但不执行它
- javascript - elasticsearch中如何将url参数添加到elasticsearch UpdateByQuery
- google-apps-script - 当我在单元格 A1 中进行任何更改时,如何将单元格 A2 的值更改为“Today()”?
- php - 在使用 php 向用户显示之前更改网页内容
- c# - 转换为 JSON 时,@ 符号出现在 XML 的属性之前
- javascript - Vanilla Javascript 类中的“计算属性”