Flutter 访问API响应并在按钮上打印,点击颤振?
在登录API中,它返回响应,我想在按钮onclick中获取响应并打印它 我该怎么做Flutter 访问API响应并在按钮上打印,点击颤振?,flutter,dart,future,Flutter,Dart,Future,在登录API中,它返回响应,我想在按钮onclick中获取响应并打印它 我该怎么做 onTap: () async => { await propertyBloc.fetchAllCategory(); <some code> }, onTap:()异步=>{ 等待propertyBloc.fetchAllCategory(); }, 要完成任务,您必须执行以下操作: 调用API并获取响应 即兴打印 1
onTap: () async => {
await propertyBloc.fetchAllCategory();
<some code>
},
onTap:()异步=>{
等待propertyBloc.fetchAllCategory();
},
要完成任务,您必须执行以下操作:
import 'package:http/http.dart' as http;
Future<http.Response> fetchData() async {
final response =
await http.get(your_url);
if (response.statusCode == 200) {
// If server returns an OK response, parse the JSON.
return json.decode(response.body);
} else {
// If that response was not OK, throw an error.
throw Exception('Failed to load post');
}
}
希望有帮助。:) 一切都很好,我可以通过stream builder访问数据。但是我怎样才能在button onClick中访问它呢?我必须将它存储在另一个变量或本地存储器中。在我的表示中,当你点击按钮时,你将能够看到数据。进行打印并登录到控制台。
//Call the function on tap of the button, which will return the data
onTap: (){this.fetchData();}