Server response with output from Future Object


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 ''.
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;

  var handler = const shelf.Pipeline()

  var server = await io.serve(handler, _hostname, port);
  print('Serving at http://${}:${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'});

Answered By – julemand101

Answer Checked By – Willingham (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *