Reputation:
Update context
Can I use where and orderby in FirebaseFirestore to running? Because I got the error in itemCount: snapshot.data!.docs.length
when I use this code on below...
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],
),
),
);
},
),
So...where has problem? when I mark the .where('country', isEqualTo: '${user.country}')
it's working, but can't use both...
Update context
When I want to get data from firebase, I tried to used this methods, but I get a error code _CastError (Null check operator used on a null value)
.
User get getUser => _user!;
If I mark .where('country', isEqualTo: '${user.country}')
or .orderBy('time', descending: true)
, it was working, but they can't appear at the same time.
So how can do use both?
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();
}
}
Upvotes: 0
Views: 396
Reputation: 388
You're not updating _user
.
You need to call
Provider.of<UserProvider>(context).refreshUser()
at least i think that's how you use provider.
So it should be
final width = MediaQuery.of(context).size.width;
Provider.of<UserProvider>(context).refreshUser();
final User user = Provider.of<UserProvider>(context).getUser;
That should update/set the value of _user
Upvotes: 0