1
resposta

Seu futuro ainda não está escrito [Meu Código]

import 'dart:io';

class Music {
  List<List<String>> lyrics = [
    [
      "Carry On Wayward Son - Kansas",
    ],
    [
      "Carry on, my wayward son,",
      "There'll be peace when you are done,",
      "Lay your weary head to rest,",
      "Don't you cry no more,",
    ],
    [
      "Once I rose above the noise and confusion,",
      "Just to get a glimpse beyond this illusion,",
      "I was soaring ever higher,",
      "But I flew too high,",
    ],
    [
      "Though my eyes could see, I still was a blind man,",
      "Though my mind could think, I still was a mad man,",
      "I hear the voices when I'm dreaming,",
      "I can hear them say,",
    ],
    [
      "Carry on, my wayward son,",
      "There'll be peace when you are done,",
      "Lay your weary head to rest,",
      "Don't you cry no more,",
    ],
    [
      "Masquerading as a man with a reason,",
      "My charade is the event of the season,",
      "And if I claim to be a wise man, well,",
      "It surely means that I don't know,",
    ],
    [
      "On a stormy sea of moving emotion,",
      "Tossed about, I'm like a ship on the ocean,",
      "I set a course for winds of fortune,",
      "But I hear the voices say,",
    ],
    [
      "Carry on, my wayward son,",
      "There'll be peace when you are done,",
      "Lay your weary head to rest,",
      "Don't you cry no more, no,",
    ],
    [
      "(Carry on) you will always remember,",
      "(Carry on) nothing equals the splendor,",
      "Now your life's no longer empty,",
      "But surely heaven waits for you,",
    ],
    [
      "Carry on, my wayward son,",
      "There'll be peace when you are done,",
      "Lay your weary head to rest,",
      "(Don't you cry) don't you cry no more,",
    ],
    [
      "No more...",
    ]
  ];

  Future<void> play() async {
    for (List<String> verse in lyrics) {
      for (String line in verse) {
        await printLineByLine(line);
        print("");
      }
      print("");
    }
  }

  Future<void> printLineByLine(String line) async {
    for (int i = 0; i < line.length; i++) {
      stdout.write(line[i]);
      await Future.delayed(Duration(milliseconds: 1));
    }
  }
}


//main 
import 'music.dart';

void main() async {
  try {
    Music music = Music();
    await music.play();
  } catch (e) {
    print("ocorreu um erro ao executar a musica");
    return;
  } finally {
    print("the end");
  }
}
1 resposta

Oii, tudo bem?

Paulo, seu código está bem organizado e a função de imprimir cada linha letra por letra está funcionando corretamente com o uso de Future.delayed.

Muito obrigada por compartilhar com a gente!

Continue assim!

Um abraço e bons estudos.