I'm using provider for state management for my app, and I'm running into a problem, provider doesn't rebuild by ListView where I want the results
Here is my feed.dart
class Feed extends StatefulWidget {
@override
_FeedState createState() => _FeedState();
}
class _FeedState extends State<Feed> {
@override
void initState() {
PostNotifier postNotifier =
Provider.of<PostNotifier>(context, listen: false);
getGlobalPosts(postNotifier);
super.initState();
}
@override
Widget build(BuildContext context) {
AuthNotifier authNotifier =
Provider.of<AuthNotifier>(context, listen: false);
PostNotifier notifier = Provider.of<PostNotifier>(context);
return Scaffold(
body: Padding(
padding: EdgeInsets.only(left: 10, right: 10, top: 80),
child: Column(
children: <Widget>[
Expanded(
child: (notifier.postList.isEmpty) ? Center(child: CircularProgressIndicator(),) :
ListView.builder(
shrinkWrap: true,
itemBuilder: (context, index) {
return PostTile(
userName: notifier.postList[index].userName,
userDp: notifier.postList[index].userDp,
imgSrc: notifier.postList[index].imageUrl,
);
},
physics: ScrollPhysics(),
itemCount: notifier.postList.length,
),
),
],
),
),
);
}
}
class PostTile extends StatelessWidget {
final String imgSrc;
final String userName;
final String userDp;
PostTile(
{@required this.userName, @required this.userDp, @required this.imgSrc});
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(horizontal: 20),
child: Row(
children: <Widget>[
CircleAvatar(
backgroundImage: NetworkImage(
"https://cdn0.iconfinder.com/data/icons/users-android-l-lollipop-icon-pack/24/user-128.png")
),
FlatButton(
child: Text(userName),
),
Expanded(
child: Container(),
),
RaisedButton(
child: Text(
'Follow',
style: TextStyle(color: Colors.white),
),
color: Colors.blue,
onPressed: () {},
)
],
),
),
SizedBox(
height: 20,
),
Image.network(imgSrc),
SizedBox(
height: 20,
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 20),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
LikeButton(),
LikeButton(
likeBuilder: (bool isLiked) {
return Icon(
Icons.bookmark,
color: isLiked ? Colors.deepPurpleAccent : Colors.grey,
size: 30,
);
},
)
],
),
)
],
);
}
}
and my getGlobalPosts function - I get my posts from firebase and the user info too
getGlobalPosts(PostNotifier postNotifier) async {
QuerySnapshot snapshot = await Firestore.instance.collection('Posts').getDocuments();
FirebaseUser firebaseUser = await FirebaseAuth
.instance.currentUser()
.catchError((e) => print(e));
List<Post> _postList = [];
snapshot.documents.forEach((document) async {
if (firebaseUser.email != document.data["email"]) {
Post post = Post.fromMap(document.data);
//TODO: Use this to get user
await post.user.get().then((value) {
post.userName = value.data['displayName'];
post.userDp = value.data['profilePicture'];
print(post.userDp);
}).whenComplete(() {
_postList.add(post);
// print(_postList[0].userName);
print('Success');
});
} else {
print('Failed');
}
});
postNotifier.postList = _postList;
}
PostNotifier -
class PostNotifier with ChangeNotifier {
List<Post> _postList = [];
Post _currentPost;
List<Post> get postList => _postList;
Post get currentPost => _currentPost;
set postList(List<Post> postList) {
_postList = postList;
notifyListeners();
}
set currentPost(Post post) {
_currentPost = post;
notifyListeners();
}
}
I'm receiving the data but my listview doesn't show up until I hot reload, Only CircularProgress indicator is shown