Dart获取功能值

时间:2014-03-22 17:22:39

标签: dart dart-async

我试图通过自己来学习Dart,但我来自C而且我有点困惑......

我这样做:

import 'dart:io';
import 'dart:async';
import 'dart:convert';

Future <Map>    ft_get_data()
{
    File    data;

    data = new File("data.json");
    return data.exists().then((value) {
        if (!value)
        {
            print("Data does no exist...\nCreating file...");
            data.createSync();
            print("Filling it...");
            data.openWrite().write('{"index":{"content":"Helllo"}}');
            print("Operation finish");
        }
        return (1);
    }).then((value) {
        data.readAsString().then((content){
            return JSON.decode(content);
        }).catchError((e) {
            print("error");
            return (new Map());
        });
    });
}

void    main()
{
    HttpServer.bind('127.0.0.1', 8080).then((server) {
        print("Server is lauching... $server");
        server.listen((HttpRequest request) {
            request.response.statusCode = HttpStatus.ACCEPTED;
            ft_get_data().then((data_map) {
                if (data_map && data_map.isNotEmpty)
                    request.response.write(data_map['index']['content']);
                else
                    request.response.write('Not work');
            }).whenComplete(request.response.close);
        });
    }) .catchError((error) {
        print("An error : $error.");
    });
}

我正在尝试取回新地图,正如你猜测的那样,它不起作用,我得到了“不工作”的信息。当代码具有相同的功能时,它起作用了......

拜托,你能帮助我吗?

并且,指针系统为C?

void function(int *i)
{
    *i = 2;
}

int main()
{
    int i = 1;
    function(&i);
    printf("%d", i);
}
// Output is 2.

感谢您的帮助。

最终代码:

import 'dart:io';
import 'dart:async';
import 'dart:convert';

Future<Map> ft_get_data()
{
    File    data;

    data = new File("data.json");
    return data.exists()
    .then((value) {
        if (!value) {
            print("Data does no exist...\nCreating file...");
            data.createSync();
            print("Filling it...");
            data.openWrite().write('{"index":{"content":"Helllo"}}');
            print("Operation finish");
        }
    })
    .then((_) => data.readAsString())
    .then((content) => JSON.decode(content))
    .catchError((e) => new Map());
}

void        main()
{
    HttpServer.bind('127.0.0.1', 8080)
    .then((server) {
        print("Server is lauching... $server");
        server.listen((HttpRequest request) {
            request.response.statusCode = HttpStatus.ACCEPTED;
            ft_get_data()
            .then((data_map) {
                if (data_map.isNotEmpty)
                    request.response.write(data_map['index']['content']);
                else
                    request.response.write('Not work');
            })
            .whenComplete(request.response.close);
        });
    })
    .catchError((error) {
        print("An error : $error.");
    });
}

3 个答案:

答案 0 :(得分:1)

简短地说,我会说你需要

Future<Map> ft_get_data() {
  ...
  return data.exists() ...
  ...
}

并像

一样使用它
server.listen((HttpRequest request) {
  request.response.statusCode = HttpStatus.ACCEPTED;
  ft_get_data().then((data_map) {
    if (data_map && data_map.isNotEmpty) request.response.write(
        data_map['index']['content']); 
    else 
        request.response.write('Not work');
    request.response.close();
  });
});

return内的then不会从ft_get_data返回,只能从then返回 如果涉及异步通话,则无法在同步时继续通话,它会一直向下同步。

答案 1 :(得分:1)

你不能将一个然后()插入另一个。需要链接它们。否则,返回JSON.decode(data)返回到无处(主事件循环)而不是之前的&#34;然后&#34;处理

答案 2 :(得分:1)

我尝试将您的代码重建为&#34;可读&#34;格式。我还没有测试过,所以可能会有错误。对我来说,如果.then()没有嵌套,代码就更容易阅读。如果.then()开始换行,它也有助于阅读。

import 'dart:io';
import 'dart:async';
import 'dart:convert';

Future <Map>ft_get_data()
{
    File data;

    data = new File("data.json");
    data.exists() //returns true or false
    .then((value) { // value is true or false
        if (!value) {
            print("Data does no exist...\nCreating file...");
            data.createSync();
            print("Filling it...");
            data.openWrite().write('{"index":{"content":"Helllo"}}');
            print("Operation finish");
        }
    }) // this doesn't need to return anything
    .then((_) => data.readAsString()) // '_' indicates that there is no input value, returns a string. This line can removed if you add return data.readAsString(); to the last line of previous function.
    .then((content) => JSON.decode(content)); // returns decoded string, this is the output of ft_get_data()-function
//    .catchError((e) { //I believe that these errors will show in main-function's error
//      print("error");
//    });
}

void    main()
{
    HttpServer.bind('127.0.0.1', 8080)
    .then((server) {
        print("Server is lauching... $server");
        server.listen((HttpRequest request) {
            request.response.statusCode = HttpStatus.ACCEPTED;
            ft_get_data()
            .then((data_map) {
                if (data_map && data_map.isNotEmpty)
                    request.response.write(data_map['index']['content']);
                else
                    request.response.write('Not work');
            })
            .whenComplete(request.response.close);
        });
    }) 
    .catchError((error) {
        print("An error : $error.");
    });
}