在 Flutter/dart 的 Futurebuilder 中访问我的未来不起作用
Acessing my future in the Futurebuilder in Flutter/dart doesn't work
我傻了,什么都不懂了。我正在尝试获取我的 JSON 数据并将其显示在我的应用程序中,我在下面链接了所有相关代码片段,这是我的第一个 post 如果我需要添加任何其他内容,我很抱歉。
我只有两个问题。 1. 我不知道如何从我的 AnalystModel 访问我的 FutureBuilder 中的数据,我尝试了不同的方法,例如使用 FutureBuilder 并使用 snapshot.hasdata[index]['analystCount'] 访问我的数据,一切有它自己的问题,请帮助。我还使用了新版本的 flutter,所以 'snapshot.data!analystCount 不起作用。
我的第二个问题是我在下面添加的错误,试图将 添加到我的 FutureBuilder,我在互联网上发现这个错误的解决方案只是 ,use a FutureBuilder" 但我已经这样做了:
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,
);
}
}
这是我的云服务:
class IEXCloudServiceAnalysts {
Future<List<Analysts>> getData() async{
var url = Uri.parse("https://sandbox.iexapis.com/stable/time-series/CORE_ESTIMATES/TSLA?token=Tpk_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();
}
这是我的文本端点:
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,
),
);
}
}
),
我得到的错误:
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(),
^
您需要更正未来的构建器通用类型
FutureBuilder<List<Analysts>>
我傻了,什么都不懂了。我正在尝试获取我的 JSON 数据并将其显示在我的应用程序中,我在下面链接了所有相关代码片段,这是我的第一个 post 如果我需要添加任何其他内容,我很抱歉。
我只有两个问题。 1. 我不知道如何从我的 AnalystModel 访问我的 FutureBuilder 中的数据,我尝试了不同的方法,例如使用 FutureBuilder 并使用 snapshot.hasdata[index]['analystCount'] 访问我的数据,一切有它自己的问题,请帮助。我还使用了新版本的 flutter,所以 'snapshot.data!analystCount 不起作用。
我的第二个问题是我在下面添加的错误,试图将 添加到我的 FutureBuilder,我在互联网上发现这个错误的解决方案只是 ,use a FutureBuilder" 但我已经这样做了:
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,
);
}
}
这是我的云服务:
class IEXCloudServiceAnalysts {
Future<List<Analysts>> getData() async{
var url = Uri.parse("https://sandbox.iexapis.com/stable/time-series/CORE_ESTIMATES/TSLA?token=Tpk_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();
}
这是我的文本端点:
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,
),
);
}
}
),
我得到的错误:
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(),
^
您需要更正未来的构建器通用类型
FutureBuilder<List<Analysts>>