颤动得到错误代码。 _CastError(用于空值的空检查运算符)

flutter got error code. _CastError (Null check operator used on a null value)

更新上下文

我可以在 FirebaseFirestore 中使用 where 和 orderby 来 运行 吗?因为我在下面使用此代码时在 itemCount: snapshot.data!.docs.length 中出现错误...

body: StreamBuilder(
        stream: FirebaseFirestore.instance
            .collection('Posts')
            .where('country', isEqualTo: '${user.country}')
            //.where('country', isEqualTo: 'Australia')
            .orderBy('time', descending: true) 
            .snapshots(),
        builder: (context,
            AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return const Center(
              child: CircularProgressIndicator(),
            );
          }
          return ListView.builder(
            itemCount: snapshot.data!.docs.length,
            itemBuilder: (context, index) => Container(
              margin: EdgeInsets.symmetric(
                  horizontal: width > webScreenSize ? width * 0.3 : 0,
                  vertical: width > webScreenSize ? 15 : 0),
              child: PostCard(
                snap: snapshot.data!.docs[index],
              ),
            ),
          );
        },
      ),

所以...哪里有问题?当我标记 .where('country', isEqualTo: '${user.country}') 它正在工作时,但不能同时使用两者...

更新上下文

当我想从 firebase 获取数据时,我尝试使用这种方法,但是我得到一个错误代码 _CastError (Null check operator used on a null value)

User get getUser => _user!;

如果我标记.where('country', isEqualTo: '${user.country}').orderBy('time', descending: true),它是有效的,但它们不能同时出现。

那么如何同时使用两者呢?

class _FeedScreenState extends State<FeedScreen> {
  @override
  Widget build(BuildContext context) {
    final width = MediaQuery.of(context).size.width;
    final User user = Provider.of<UserProvider>(context).getUser;
    return Scaffold(
body: StreamBuilder(
        stream: FirebaseFirestore.instance
            .collection('Posts')
            .where('country', isEqualTo: '${user.country}')
            .orderBy('time', descending: true)
            .snapshots(),
        builder: (context,
            AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return const Center(
              child: CircularProgressIndicator(),
            );
          }
          return ListView.builder(
            itemCount: snapshot.data!.docs.length,
            itemBuilder: (context, index) => Container(
              margin: EdgeInsets.symmetric(
                  horizontal: width > webScreenSize ? width * 0.3 : 0,
                  vertical: width > webScreenSize ? 15 : 0),
              child: PostCard(
                snap: snapshot.data!.docs[index],
              ),
            ),
          );
        },
      ),

UserProvider.dart

class UserProvider with ChangeNotifier {
  User? _user;
  final AuthMethods _authMethods = AuthMethods();

  User get getUser => _user!;

  Future<void> refreshUser() async {
    User user = await _authMethods.getUserDetails();
    _user = user;
    notifyListeners();
  }
}

您没有更新 _user

你需要打电话
Provider.of<UserProvider>(context).refreshUser() 至少我认为这就是你使用 provider 的方式。

所以应该是

    final width =  MediaQuery.of(context).size.width;
    Provider.of<UserProvider>(context).refreshUser();
    final User user = Provider.of<UserProvider>(context).getUser;

那应该update/set的值_user