0

When i refresh the data isnt fetching new data please help me

This is my method to fetch data from news org api

Future<List<Article>> getApi() async {
Response response = await get(Uri.parse(
    "https://newsapi.org/v2/top-headlines?country=us&category=business&apiKey=apikey"));

if (response.statusCode == 200) {
  Map<String, dynamic> json = jsonDecode(response.body);

  List<dynamic> body = json['articles'];
  List<Article> article = body.map((e) => Article.fromJson(e)).toList();
  return article;
} else {
  throw ("cant get the articles");
}

}

this is my builder to show data

      body: FutureBuilder<List<Article>>(
      future: futureWords,
      builder: (context, AsyncSnapshot<List<Article>> snap) {
        if (snap.hasData) {
          List<Article> articles = snap.data!;

          return RefreshIndicator(
            onRefresh: () {
              setState(() {});
              return _pullRefresh();
            },
            child: ListView.builder(
                itemCount: 20,
                itemBuilder: (context, index) {
                  return customListTile(articles[index]);
                }),
          );
        } else {
          return Center(child: CircularProgressIndicator());
        }
      }),

this is my pullrefresh method

Future<List<Article>> _pullRefresh() async {
List<Article> freshWords = await news.getApi();
setState(() {
  futureWords = Future.value(freshWords);
});

return futureWords!;

}

  • 1
    possibly duplicate of https://stackoverflow.com/questions/53170330/reload-data-when-using-futurebuilder – Malbolged Jan 06 '22 at 17:04
  • Does this answer your question? [Reload data when using FutureBuilder](https://stackoverflow.com/questions/53170330/reload-data-when-using-futurebuilder) – Malbolged Jan 06 '22 at 17:04

1 Answers1

0

May be it'll help you. If not - post your full code)

import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';

class Test2 extends StatefulWidget {
  const Test2({Key? key}) : super(key: key);

  @override
  _Test2State createState() => _Test2State();
}

class Article {
  String header='';
}

class _Test2State extends State<Test2> {
  Future <List<Article>>? futureWords;

  @override
  void initState() {
    super.initState();
    _getNews();
  }

  _getNews() async {
    var response = await http.get(Uri.parse(
        "https://newsapi.org/v2/top-headlines?country=us&category=business&apiKey=aa20aef042a14de5b99a7f7d32952504"));
    if (response.statusCode == 200) {
      Map<String, dynamic> json = jsonDecode(response.body);
      List<dynamic> body = json['articles'];
      //List<Article> articles = body.map((e) => Article.fromJson(e)).toList();
      List<Article> articles = [];
      for (var el in body) {
        Article article = Article();
        article.header = el['title'];
        articles.add(article);
      }
      articles.shuffle();
      setState(() {
        futureWords = Future.value(articles);
      });
    } else {
      throw ("cant get the articles. statusCode ${response.statusCode}");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FutureBuilder<List<Article>>(
          future: futureWords,
          builder: (context, AsyncSnapshot<List<Article>> snap) {
            if (snap.hasData) {
              List<Article> articles = snap.data!;
              return
                RefreshIndicator(
                  onRefresh: () {
                    _getNews();
                    return futureWords!;
                  },
                  child: ListView.builder(
                    itemCount: 10,
                    itemBuilder: (context, index) {
                      return ListTile(title: Text(articles[index].header));
                    }),
                );
            } else {
              return Center(child: CircularProgressIndicator());
            }
          }),
    );
  }
}