简体   繁体   中英

Return String from Future Object

I am trying to fetch html page via localproxy to parse and get urls from it. I can't find any library which works without Future in Dart. So i have difficulty returning String from a Future Object. Below is full code of dart file.

import 'package:universal_html/driver.dart';
import 'package:universal_html/prefer_universal/html.dart';

String proxyUrl='http://localhost/php-proxy/index.php?q=';
String hallTicketUrl='http://www.ignou.ac.in/ignou/studentzone/results/6';

Future<String> getList()async{
  final driver = HtmlDriver();
  await driver.setDocumentFromUri(Uri.parse(proxyUrl+hallTicketUrl));
  final items = driver.document.querySelectorAll('.middleconten2column a');

  Element urls=Element.tag('urls');
  items.forEach((item)=>urls.append(Element.tag('url')
                        ..setAttribute('href',item.getAttribute('href'))
                        ..text=item.text
                        )
        );
  print('${items.length} items found');

return Future.value(urls.outerHtml);
}

String Handler(String app){
    switch(app){
       case 'list': return getList() as String;
}
return "";
}

main(){
    print(Handler('list'));
}

I think you have misunderstood what the async keyword does to methods. When a method are marked as async it will always automatically returns a Future of something. That is the reason why you need to specify eg Future<String> as the return type.

But because the creation are done "automatically" you don't really need to do the following:

return Future.value(urls.outerHtml);

But can just do:

return urls.outerHtml;

After this your have discovered the problem with using asynchronous programming where everything in your call stack needs to be marked as async since you have an await somewhere in your code.

I have fixed your code so it now hope it works correctly with all the future stuff:

import 'package:universal_html/driver.dart';
import 'package:universal_html/prefer_universal/html.dart';

String proxyUrl = 'http://localhost/php-proxy/index.php?q=';
String hallTicketUrl = 'http://www.ignou.ac.in/ignou/studentzone/results/6';

Future<String> getList() async {
  final driver = HtmlDriver();
  await driver.setDocumentFromUri(Uri.parse(proxyUrl + hallTicketUrl));
  final items = driver.document.querySelectorAll('.middleconten2column a');

  Element urls = Element.tag('urls');
  items.forEach((item) => urls.append(Element.tag('url')
    ..setAttribute('href', item.getAttribute('href'))
    ..text = item.text));
  print('${items.length} items found');

  return urls.outerHtml;
}

Future<String> Handler(String app) async {
  switch (app) {
    case 'list':
      return await getList();
  }
  return "";
}

main() async {
  print(await Handler('list'));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM