0

I'm designing an app that is in some ways similar to Facebook's app. What I would like to emulate is how they handle the updating for posts in the homepage.

For example, say I scroll down to my friend Lisa's post, which she posted 1 minute ago. If I just stare at the screen for 2 minutes, it will still say that it was posted 1 minute ago. However, if I scroll down to another post, and then immediately scroll back to hers it will correctly state that it was posted 3 minutes ago.

I would like to achieve something similar, but I have no idea about how to implement it or how to search for it. Just imagine I'm making a call to the server as I scroll towards it and only 1 entire post is visible at a time.

  • can you please more Explain and add an Image... I a little Weak in English.we Solve it Together :). – Sajjad Aug 18 '20 at 03:40

2 Answers2

0

I think you need to use a Timer.periodic for set state every 1 minute ... some thing like this Flutter Countdown Timer

void startTimer() {
const oneMin = const Duration(Minute: 1);
_timer = new Timer.periodic(
oneMin,
(Timer timer) => setState(() {},), );
}

and call this method at Init state...this Code is Just for Idea may be not working ! ... the Link Example is better.

Sajjad
  • 2,593
  • 16
  • 26
  • No, this is not what I asked about. I asked about how to update it when it's in view, like in Facebook's example. – Joney Laus Aug 17 '20 at 07:41
0

I'm not sure which database you're using, What I understand from your question is you want to read data from database and show it all the way in listview. full code here! by @seanmavley

     StreamController _postsController;
     int count = 1;

 Future fetchPost([howMany = 5]) async {
final response = await http.get(
    'https://blog.khophi.co/wp-json/wp/v2/posts/?per_page=$howMany&context=embed');

if (response.statusCode == 200) {
  return json.decode(response.body);
} else {
  throw Exception('Failed to load post');
}
}

loadPosts() async {
fetchPost().then((res) async {
  _postsController.add(res);
  return res;
});
}

showSnack() {
return scaffoldKey.currentState.showSnackBar(
  SnackBar(
    content: Text('New content loaded'),
  ),
);
}

Future<Null> _handleRefresh() async {
count++;
print(count);
fetchPost(count * 5).then((res) async {
  _postsController.add(res);
  showSnack();
  return null;
});
}
  //inside the scaffold
  body: StreamBuilder(
    stream: _postsController.stream,
    builder: (BuildContext context, AsyncSnapshot snapshot) {
      print('Has error: ${snapshot.hasError}');
      print('Has data: ${snapshot.hasData}');
      print('Snapshot Data ${snapshot.data}');

      if (snapshot.hasError) {
        return Text(snapshot.error);
      }

      if (snapshot.hasData) {
        return Column(
          children: <Widget>[
            Expanded(
              child: Scrollbar(
                child: RefreshIndicator(
                  onRefresh: _handleRefresh,
                  child: ListView.builder(
                    physics: const AlwaysScrollableScrollPhysics(),
                    itemCount: snapshot.data.length,
                    itemBuilder: (context, index) {
                      var post = snapshot.data[index];
                      return ListTile(
                        title: Text(post['title']['rendered']),
                        subtitle: Text(post['date']),
                      );
                    },
                  ),
                ),
              ),
            ),
          ],
        );
      }
Ayyaz meo
  • 450
  • 5
  • 13
  • What I'm specifically wondering about is how I make sure that a certain post is ONLY updated when it's seen by the user. – Joney Laus Aug 18 '20 at 16:48