0

I have the logic of working with the API in the same file with the body for the data it receives. How can I put the Future function, which takes the data into a separate file, correctly? My Future code:

//fetch data from API
  Future<List<CurrencyModel>?> _fetchCurrency() async {
    currencyList = [];
    final response = await http.get(
      Uri.parse(
          'https:...'),
    );
    if (response.statusCode == 200) {
      List<dynamic> values = [];
      values = json.decode(response.body);
      if (values.isNotEmpty) {
        for (int i = 0; i < values.length; i++) {
          if (values[i] != null) {
            Map<String, dynamic> map = values[i];
            currencyList.add(
              CurrencyModel.fromJson(map),
            );
          }
        }
        setState(() {
          currencyList;
        });
      }
      return currencyList;
    } else {
      throw Exception('Failed to load currencies');
    }
  }
userName
  • 903
  • 2
  • 20
  • If you getting data from API and display it into Flutter. refer my answers [Answer 1](https://stackoverflow.com/a/68533647/13997210), [Answer 2](https://stackoverflow.com/a/68807671/13997210), [Answer 3](https://stackoverflow.com/a/69131277/13997210), [Answer 4](https://stackoverflow.com/a/68709502/13997210), [Answer 5](https://stackoverflow.com/a/68594656/13997210) and official documentation [here](https://docs.flutter.dev/cookbook/networking/fetch-data) – Ravindra S. Patil May 09 '22 at 09:46

1 Answers1

1

you can use future builder in all different widget

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 10 '22 at 00:00