How do I read console input / stdin in Dart?

前端 未结 4 865
你的背包
你的背包 2021-02-03 21:58

How do I read console input from stdin in Dart?

Is there a scanf in Dart?

4条回答
  •  太阳男子
    2021-02-03 22:46

    With M3 dart classes like StringInputStream are replaced with Stream, try this:

    import 'dart:io';
    import 'dart:async';
    
    void main() {
      print("Please, enter a line \n");
      Stream cmdLine = stdin
          .transform(new StringDecoder())
          .transform(new LineTransformer());
    
      StreamSubscription cmdSubscription = cmdLine.listen(
        (line) => print('Entered line: $line '),
        onDone: () => print(' finished'),
        onError: (e) => /* Error on input. */);
    
    
    }
    

提交回复
热议问题