Why is my streambuilder code suddenly not working?

Viewed 40

I was working on my prayer check in app and I have a widget where it shows how many time left until the next prayer. Basically a countdown timer to the next prayer . The code was working fine for weeks already and i have not touched the code at all, but suddenly it only shows null. Originally, it was showing hours:minutes:seconds but now only shows null

import 'package:flutter/material.dart';
import 'package:adhan/adhan.dart';

class countdown extends StatefulWidget {
  @override
  State<countdown> createState() => _countdownState();
}

class _countdownState extends State<countdown> {
  late PrayerTimes prayerTimes;
  final date = DateComponents.from(DateTime.now());
  final myCoordinates = Coordinates(3.139003, 101.686855);
  // Replace with your own location lat, lng.
  final params = CalculationMethod.karachi.getParameters();

  @override
  void initState() {
    prayerTimes = PrayerTimes.today(myCoordinates, params);
    super.initState();
  }

  secondToHour(int seconds) {
    int minutes = seconds ~/ 60;
    int hours = minutes ~/ 60;
    seconds = seconds - minutes * 60;
    minutes = minutes - hours * 60;
    
    return "$hours Jam :$minutes Min :$seconds Saat";
  }

  @override
  Widget build(BuildContext context) {
    diffTime() async* {
      yield* Stream.periodic(Duration(seconds: 1), (t) {
        DateTime now = DateTime.now();
        if (prayerTimes.currentPrayer().index == 1) {
          DateTime nextPrayertime = prayerTimes.sunrise;
          DateTime dt1 = DateTime.parse("${nextPrayertime}");
          DateTime dt2 = DateTime.parse("${now}");
          Duration diff = dt1.difference(dt2);

          return secondToHour(diff.inSeconds);
        } else if (prayerTimes.currentPrayer().index == 3) {
          DateTime nextPrayertime = prayerTimes.asr;
          DateTime dt1 = DateTime.parse("${nextPrayertime}");
          DateTime dt2 = DateTime.parse("${now}");
          Duration diff = dt1.difference(dt2);

          return secondToHour(diff.inSeconds);
        } else if (prayerTimes.currentPrayer().index == 4) {
          DateTime nextPrayertime = prayerTimes.maghrib;
          DateTime dt1 = DateTime.parse("${nextPrayertime}");
          DateTime dt2 = DateTime.parse("${now}");
          Duration diff = dt1.difference(dt2);

          return secondToHour(diff.inSeconds);
        } else if (prayerTimes.currentPrayer().index == 5) {
          DateTime nextPrayertime = prayerTimes.isha;
          DateTime dt1 = DateTime.parse("${nextPrayertime}");
          DateTime dt2 = DateTime.parse("${now}");
          Duration diff = dt1.difference(dt2);
          
          return secondToHour(diff.inSeconds);
        } else if (prayerTimes.currentPrayer().index == 6) {
          return "Last Prayer for today";
        } else {
          return "Wait for next prayer";
        }
      });
    }

    return Container(
        child: StreamBuilder(
            stream: diffTime(),
            builder: (context, snapshot) {
              return Text(
                "${snapshot.data}",
                style: TextStyle(
                  fontSize: 18,
                  color: Colors.black,
                  fontWeight: FontWeight.w500,
                ),
              );

              // Text("DT1:" + dt1.toString()),
              // Text("DT2:" + dt2.toString()),

              // Text("Difference in Days: " + diff.inDays.toString()),
              // Text("Difference in Hours: " + diff.inHours.toString()),
              // Text("Difference in Minutes: " + diff.inMinutes.toString()),
            }));
  }
}

I think the problem may lies in the yield code but im still a beginner and dont know the right solution.

0 Answers
Related