2

我正在尝试自学 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.");
    });
}
4

3 回答 3

1

you cannot insert one then() into the other. Need to chain them. Otherwise, return JSON.decode(data) returns to nowhere (main event loop) instead of previous "then" handler

于 2014-03-23T03:39:28.410 回答
1

我试图将您的代码重建为“可读”格式。我没有测试它,所以可能会有错误。.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.");
    });
}
于 2014-03-23T06:42:47.400 回答
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();
  });
});

areturn内部的Athen不会从返回,ft_get_data而只会从返回then 如果涉及异步调用,如果它是同步的,则您无法继续,那么它一直是异步的。

于 2014-03-22T17:34:53.360 回答