0

i have school project,we are currently doing voice controlled kiosk and the navigation through app will be with voice commands,im doing the application part but im having hard times with integrating with ml team,so the thing is they take the voice command and write it to txt file,the process goes like write one-wait for another one-delete existing one-write the new one.

i coded this but then i learned future reads just once,it doesnt update the value when file is changed,im new to flutter and my programming skills are not good enough,i really need help,i think i need to use stream and i tried to convert it to stream but couldnt succeded so yeah i want to learn how to read that txt file constantly and then update the value when new one came

class _MainScreenState extends State<MainScreen> {
List<String> _questions = [];
String answer;

Future<List<String>> _loadQuestions() async {
  List<String> questions = [];
  await rootBundle.loadString('assets/ml.txt').then((q) {
    for (String i in LineSplitter().convert(q)) {
      questions.add(i);
    }
  });
  return questions;
}

Timer timer;
@override
void initState() {
  _setup();

  super.initState();
  timer = Timer.periodic(Duration(seconds: 5), (Timer t) => navigate());
}

_setup() async {
  // Retrieve the questions (Processed in the background)
  List<String> questions = await _loadQuestions();

  // Notify the UI and display the questions
  setState(() {
    _questions = questions;
  });
}

void navigate() async {
  answer = _questions[0];

  print(answer);
  setState(() {
    if (answer == 'one') {
      Navigator.push(
          context, MaterialPageRoute(builder: (context) => Majors()));
      timer.cancel();
    } else if (answer == 'two') {
      Navigator.push(
          context, MaterialPageRoute(builder: (context) => Administrative()));
    } else if (answer == 'three') {
      Navigator.push(
          context, MaterialPageRoute(builder: (context) => Classes()));
    }
  });
}
 Widget build(BuildContext context) {
  • Does this answer your question? [Flutter reading a file as a stream](https://stackoverflow.com/questions/51136512/flutter-reading-a-file-as-a-stream) – TarHalda Oct 27 '22 at 14:02

1 Answers1

0

The easiest solution seems to be to call _setup every time the timer elapses before you call navigate. That will read the file again so your code can react to changes. However that of course reads the file whether it's been changed or not.

Optimally you would want to listen to changes to the file and only read it again when it's actually been changed. There are options to "watch" for changes in the filesystem. Dart provides a watch method and there's also a package for that. The exact functionality of this watcher is however dependent on the OS.

Alex Schneider
  • 364
  • 2
  • 7
  • Appreciated for quick reply,but timer method didnt work out, tried to use watcher package couldnt integrated my system like i said earlier im not good at flutter,im gonna try again tonight but if you have any further advice,would love to listen – Oğuzhan Bagirci Jun 07 '21 at 16:15