gpt4 book ai didi

string - 获取互联网上文​​本文件的内容, flutter 朔迷离

转载 作者:行者123 更新时间:2023-12-03 04:35:10 24 4
gpt4 key购买 nike

当我读取互联网上url上存在的文本文件的内容时,它就在函数内部工作。
这是我的职责

String x="";
Future<String> ip(String url)
async {
HttpClient client = new HttpClient();
var request = await client.postUrl(Uri.parse(url));
request.close().then((response) {
utf8.decoder.bind(response.cast<List<int>>()).listen((content) {
print(content);// output https://st03.sslstream.dlf.de/dlf/03/128/mp3/stream.mp3
setState(() {
x = content; // value null
});
});
});
}
所以当我打印此文件的内容时
http://opml.radiotime.com/Tune.ashx?id=s120806
结果将是
https://st03.sslstream.dlf.de/dlf/03/128/mp3/stream.mp3
但是正如您在我的函数中看到的那样,我将内容的值赋予了x字段
setState(() {
x = content; // value null
});
因此,当我以initstate调用Function并打印x时,该值为null!
  @override
void initState() {
ip("http://opml.radiotime.com/Tune.ashx?id=s120806"); //output https://st03.sslstream.dlf.de/dlf/03/128/mp3/stream.mp3
print(x); // output null
super.initState();
audioStart();
}
所以我需要给其他字段或属性响应的内容,或者我想让我的函数返回一个字符串而不是一个Future,并且这个字符串实际上是作为字符串的The Response的内容,而不是null。
提前致谢

最佳答案

试试这个,

  @override
void initState() {
initAsyncState();
}

void initAsyncState() async {
await ip("http://opml.radiotime.com/Tune.ashx?id=s120806"); //output https://st03.sslstream.dlf.de/dlf/03/128/mp3/stream.mp3
print(x); // output will not be null
}
如果要在小部件中使用响应,请使用 FutureBuilder

关于string - 获取互联网上文​​本文件的内容, flutter 朔迷离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64290967/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com