0

我是颤振的新手,我正在尝试将产品添加到购物车,但是当我单击添加按钮时,它显示该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

我希望你能帮助我提前谢谢你感谢它。

4

1 回答 1

1

发生这种情况是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也这样做(例如FirebaseAuthSharedPreferences等)

于 2021-06-02T10:37:46.113 回答