不良状态:流已经听过Flutter错误 [英] Bad state: Stream has already been listened to Flutter error

查看:62
本文介绍了不良状态:流已经听过Flutter错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在呼叫一个api。发送请求后,我得到了流式响应。但是我无法解析响应并将其转换为String / JSON。

I am calling an api. I am getting a streamed response after sending the request. But i cannot parse the response and convert it to String/JSON. This is where I am calling the api.

static Future<String> callDeviceListFetchApi() async {
Completer completer = new Completer();
String jsonResponse;
String url = Constants.BASE_URL + Constants.DEVICE_REGISTER_URL;
var client = new http.Client();
var request = new http.Request('GET', Uri.parse(url));
request.headers[HttpHeaders.CONTENT_TYPE] = 'application/json';
request.headers[HttpHeaders.AUTHORIZATION] = '<auth code>';
await client.send(request).then((response) {
  response.stream.bytesToString().then((value) {
    print(value.toString());
    jsonResponse = value.toString();
    completer.complete(jsonResponse);
  });
}).catchError((error) {
  print(error.toString());
});
return completer.future;
}

我遇到了错误,
不良状态:Stream已被监听 Flutter错误。知道为什么会这样吗?

I am getting the error, Bad state: Stream has already been listened to Flutter error. Any idea why this is happening?

推荐答案

您的代码有些错误。我认为您对异步和期货在dart中的工作方式有一些误解-您应该重新阅读 docs 和本教程(第1部分第2部分)。

There's a couple of things wrong with your code. I think you have a slight misunderstanding about how Async and Futures work in dart - you should re-read the docs and this tutorial (part 1 and part 2).

,问题是您从异步函数返回了未来。如果您从异步函数返回的将来,它将有问题(我不知道为什么分析器无法捕获到该结果)。

Basically, the problem is that you were returning a 'Future' from an async function. If you return a future from an async function, it has issues (I don't know why the analyzer doesn't catch that).

Future<String> callDeviceListFetchApi() async {
  Completer completer = new Completer();
  String url = "<url>";
  var client = new http.Client();
  var request = new http.Request('GET', Uri.parse(url));
  request.headers[HttpHeaders.CONTENT_TYPE] = 'application/json';
  request.headers[HttpHeaders.AUTHORIZATION] =
      '<auth string>';
  var response = await client.send(request);
  String jsonResponse;
  try {
    var value = await response.stream.bytesToString();
    print(value.toString());
    jsonResponse = value.toString();
  } catch (error) {
    print(error.toString());
  }
  return completer.complete(jsonResponse);
}

还是不异步:

Future<String> callDeviceListFetchApiNotAsync() {
  String url = "<url>";
  var client = new http.Client();
  var request = new http.Request('GET', Uri.parse(url));
  request.headers[HttpHeaders.CONTENT_TYPE] = 'application/json';
  request.headers[HttpHeaders.AUTHORIZATION] =
      '<auth string>';
  Completer completer = new Completer();

  return client.send(request).then((response) {
    return response.stream.bytesToString();
  }).then((value) {
    print(value.toString());
    return value.toString();
  }).catchError((error) {
    print(error.toString());
    // if you use catchError, whatever you return from it
    // is the value you'll get wherever you resolve the future.
    return null;
  });
}

但是,除非您尝试执行我没有看到的操作,否则一种更简单的方法(假设您要做的就是从服务器获取字符串):

But unless you're trying to do something I'm not seeing, there's a way easier way to do this (assuming all you want to do is get a string from a server):

Future<String> getList() async {
  var response = await http.get("<url>", headers: {
    HttpHeaders.CONTENT_TYPE: 'application/json', 
    HttpHeaders.AUTHORIZATION: '<auth string>',
  });
  if (response.statusCode == 200) {
    return response.body;
  } else {
    throw Error();
  }
}

这篇关于不良状态:流已经听过Flutter错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆