服务器响应与Future对象的输出 [英] Server response with output from Future Object
问题描述
import‘dart:io’;
导入 package:args / args.dart;
导入 package:shelf / shelf.dart作为货架;
作为 io导入 package:shelf / shelf_io.dart
以ht格式导入 HallTicket.dart;
//对于Google Cloud Run,将_hostname设置为 0.0.0.0。
const _hostname ='localhost';
main(List< String> args)异步{
var parser = ArgParser().. addOption('port',abbr:'p');
var结果= parser.parse(args);
//对于Google Cloud Run,我们尊重PORT环境变量
var portStr = result ['port']? Platform.environment ['PORT'] ?? ‘8080’;
var port = int.tryParse(portStr);
if(port == null){
stdout.writeln(无法将端口值 $ portStr解析为数字。);
// 64:命令行使用错误
exitCode = 64;
的回报;
}
var handler = const rack.Pipeline()
.addMiddleware(shelf.logRequests())
.addHandler(_echoRequest);
var服务器=等待io.serve(处理程序,_hostname,port);
print(在http:// $ {server.address.host}:$ {server.port}上提供服务);
}
Future< shelf.Response> _echoRequest(shelf.Request request)async {
rack.Response.ok(’请求 $ {request.url} \n’+等待ht.handler(’list’));
}
分析仪为您提供以下内容 _echoRequest
方法的警告:
info:此函数的返回类型为未来,但
不会以return语句结尾。
如果您检查 addHandler
您将看到它期望返回一个处理程序。
因此,您需要添加返回值以使其在我的机器上:
Future< shelf.Response> _echoRequest(shelf.Request request)异步{
return rack.Response.ok(
'请求 $ {request.url} \n'+等待ht.handler('list2'),
标头:{'Content-Type':'text / html'});
}
i created a async/await function in another file thus its handler is returning a Future Object. Now i can't understand how to give response to client with content of that Future Object in Dart. I am using basic dart server with shelf package.Below is code where ht.handler('list') returns a Future Object and i want to send that string to client as response. But i am getting internal server error.
import 'dart:io';
import 'package:args/args.dart';
import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf/shelf_io.dart' as io;
import 'HallTicket.dart' as ht;
// For Google Cloud Run, set _hostname to '0.0.0.0'.
const _hostname = 'localhost';
main(List<String> args) async {
var parser = ArgParser()..addOption('port', abbr: 'p');
var result = parser.parse(args);
// For Google Cloud Run, we respect the PORT environment variable
var portStr = result['port'] ?? Platform.environment['PORT'] ?? '8080';
var port = int.tryParse(portStr);
if (port == null) {
stdout.writeln('Could not parse port value "$portStr" into a number.');
// 64: command line usage error
exitCode = 64;
return;
}
var handler = const shelf.Pipeline()
.addMiddleware(shelf.logRequests())
.addHandler(_echoRequest);
var server = await io.serve(handler, _hostname, port);
print('Serving at http://${server.address.host}:${server.port}');
}
Future<shelf.Response> _echoRequest(shelf.Request request)async{
shelf.Response.ok('Request for "${request.url}"\n'+await ht.handler('list'));
}
The analyzer gives your the following warning for your _echoRequest
method:
info: This function has a return type of 'Future', but doesn't end with a return statement.
And if you check the requirement for addHandler
you will see it expects a handler to be returned.
So you need to add the return which makes it work on my machine:
Future<shelf.Response> _echoRequest(shelf.Request request) async {
return shelf.Response.ok(
'Request for "${request.url}"\n' + await ht.handler('list2'),
headers: {'Content-Type': 'text/html'});
}
这篇关于服务器响应与Future对象的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!