Flutter:访问进程标准输出并流式传输到 StreamBuilder 小部件
Flutter: Access Process stdout and stream to StreamBuilder widget
我希望能够使用 await Process.start()
启动进程并能够在进程执行期间访问标准输出并将输出发送到 StreamBuilder 小部件。
StreamBuilder 小部件
StreamBuilder(
stream: snapshot.data,
builder: (BuildContext context, AsyncSnapshot stream) {
print('Connection State: ${stream.connectionState}');
switch (stream.connectionState) {
case ConnectionState.done:
return SingleChildScrollView(
controller: controller,
reverse: true,
child: Text(
executionOutput,
));
default:
executionOutput += '\n${stream.data}';
return SingleChildScrollView(
controller: controller,
reverse: true,
child: Text(
executionOutput,
));
这是我的流代码,我想每秒检查一次标准输出,直到进程完成
Stream getOutput() async* {
var p = await Process.start('python.exe', ['streamData.py']);
while (true) {
await Future.delayed(Duration(seconds: 1));
yield p.stdout.transform(utf8.decoder).toString();
}
}
使用yield*
将流委托给另一个流。
import 'dart:convert';
import 'dart:io';
void main(List<String> args) {
getOutput().listen(print);
}
Stream getOutput() async* {
var p = await Process.start('tail', ['-f', '/path/to/log']);
yield* p.stdout.transform(utf8.decoder);
}
我希望能够使用 await Process.start()
启动进程并能够在进程执行期间访问标准输出并将输出发送到 StreamBuilder 小部件。
StreamBuilder 小部件
StreamBuilder(
stream: snapshot.data,
builder: (BuildContext context, AsyncSnapshot stream) {
print('Connection State: ${stream.connectionState}');
switch (stream.connectionState) {
case ConnectionState.done:
return SingleChildScrollView(
controller: controller,
reverse: true,
child: Text(
executionOutput,
));
default:
executionOutput += '\n${stream.data}';
return SingleChildScrollView(
controller: controller,
reverse: true,
child: Text(
executionOutput,
));
这是我的流代码,我想每秒检查一次标准输出,直到进程完成
Stream getOutput() async* {
var p = await Process.start('python.exe', ['streamData.py']);
while (true) {
await Future.delayed(Duration(seconds: 1));
yield p.stdout.transform(utf8.decoder).toString();
}
}
使用yield*
将流委托给另一个流。
import 'dart:convert';
import 'dart:io';
void main(List<String> args) {
getOutput().listen(print);
}
Stream getOutput() async* {
var p = await Process.start('tail', ['-f', '/path/to/log']);
yield* p.stdout.transform(utf8.decoder);
}