等待 api 中的空响应
Posted
技术标签:
【中文标题】等待 api 中的空响应【英文标题】:Null response in await api 【发布时间】:2020-07-26 04:05:56 【问题描述】:这个 print('响应>>>>>>');打印(响应);一片空白 print("api") - 这个返回就OK了
在 Api.dart 中
未来列表(参数)异步 Dio dio = new Dio(); 尝试 var arg = 参数; 字符串 refreshToken = 令牌; dio.options.baseUrl = serverUrl + "/test/" + arg.toString(); dio.options.responseType = ResponseType.json;
Response response = await dio.get( "$dio.options.baseUrl", options: Options( headers: 'Authorization': "Bearer $refreshToken", 'Content-Type': "application/json;charset=UTF-8", 'Accept': "gzip" ) ); print ("api"); print(response.data); return await response.data; catch (e) print(e);
myclass.dart
cl
ass 页面扩展 StatefulWidget @覆盖 PageState createState() => PageState();
class PageState extends State<Page> List data; Future<List> list() async Network network = new Network(); final String arguments = ModalRoute.of(context).settings.arguments as String; print(arguments); var response = await network.list(arguments.toString()); print('response>>>>>>'); print(response); if (mounted) this.setState(() data = response; ); ; // _saving = false; // print('data'); // print(data); // return "Success!"; @override void initState() super.initState(); // Future.delayed(Duration.zero, this.dados_propostas); // dados_propostas(); @override Widget build(BuildContext context) this.list(); return AlertDialog( title: const Text('Teste'), actions: <Widget>[ FlatButton( onPressed: debugDumpApp, child: Row( children: <Widget>[ const Icon( Icons.dvr, size: 18.0, ), Container( width: 8.0, ), const Text('DUMP'), ], ), ), FlatButton( onPressed: () Navigator.pop(context, false); , child: const Text('OK'), ), ], );
【问题讨论】:
【参考方案1】:使用此功能:
Future<Response> get(String url, String token) async
Response response = Response();
try
dio.options.contentType = "application/json;charset=UTF-8";
dio.options.headers["Authorization"] = "Bearer $token";
response = await dio.get(url);
return response;
on DioError catch (error, stacktrace)
print("Exception occured: $error stackTrace: $stacktrace");
return error.response;
【讨论】:
现在直到 print('response>>>>>>');打印(响应);但是,在这段代码中 if (mounted) this.setState(() data = response as Future以上是关于等待 api 中的空响应的主要内容,如果未能解决你的问题,请参考以下文章