Acessing my future in the Futurebuilder in Flutter/dart doesn’t work

Issue

I’m dumb and don’t understand anything anymore. I’m trying to get my JSON Data and show it in my app, I linked all relevant code snippets below, this is my first post so I’m sorry if I need to add anything else. I simply have 2 problems. 1. I dont know how to access the data in my FutureBuilder from my AnalystModel, I tried different ways, for example using a FutureBuilder and accessing my data by using snapshot.hasdata[index][‘analystCount’], everything had it’s own problems, please help. I also use a new version of flutter so ‘snapshot.data!analystCount won’t work. My 2nd problem is the Error I’ve added below, trying to add the to my FutureBuilder, the solution in the internet I found to this error simply was ,,use a FutureBuilder” but i already do:

class Analysts {

  final String analystCount;
  final String consensusDate;
  final String marketConsensus;
  final String marketConsensusTargetPrice;

  Analysts({this.analystCount, this.consensusDate, this.marketConsensus,
    this.marketConsensusTargetPrice,});

  factory Analysts.fromJson(Map<String, dynamic> json) {
    return Analysts(
      analystCount: json['analystCount'].toString() as String,
      consensusDate: json['consensusDate'] as String,
      marketConsensus: json['marketConsensus'].toString() as String,
      marketConsensusTargetPrice: json['marketConsensusTargetPrice'].toString() as String,
    );
  }
}

This is my Cloudservice:

class IEXCloudServiceAnalysts {

  Future<List<Analysts>> getData() async{

    var url  Uri.parse("https://sandbox.iexapis.com/stable/time-series/CORE_ESTIMATES/TSLA?tokenTpk_85b3b5cdb32147d3a0fb751cc5176cdd");
    Response res  await get(url);

    return parseAnalysis(res.body);

  }
}

List<Analysts> parseAnalysis(String responseBody) {
  final parsed  jsonDecode(responseBody).cast<Map<String, dynamic>>();
  return parsed.map<Analysts>((json) > Analysts.fromJson(json)).toList();
}

This is my endpoint for the text:

FutureBuilder<Analysts>(
                      future: iexcloudanalysts.getData(),
                      builder: (context, snapshot) {
                        if (snapshot.hasData) {
                          Analysts analysts  Analysts();
                          return Text(
                            analysts.consensusDate ?? 'Error',
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: 32,
                              fontWeight: FontWeight.bold,
                            ),
                          );
                        }
                        if(snapshot.hasError){
                          return Text(
                            'N/A',
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: 32,
                              fontWeight: FontWeight.bold,
                            ),
                          );
                        }
                        else {
                          return const Text(
                            "---",
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: 32,
                              fontWeight: FontWeight.bold,
                            ),
                          );
                        }
                      }
                  ),

The Error I get:

lib/Widgets/stock_analysts_widget.dart:63:48: Error: The argument type 'Future<List<Analysts>>' can't be assigned to the parameter type 'Future<Analysts>'.
 - 'Future' is from 'dart:async'.
 - 'List' is from 'dart:core'.
 - 'Analysts' is from 'package:flutter_app/models/analysts.dart' ('lib/models/analysts.dart').
                      future: iexcloudanalysts.getData(),
                                               ^

Solution

You need to correct future builder generic type FutureBuilder<List<Analysts>>

Answered By – Hosam Hasan

Leave a Comment