flutter make api every second code example

Example 1: repeating action every x seconds flutter

Timer timer;


void initState() {
  super.initState();
  timer = Timer.periodic(Duration(seconds: 15), (Timer t) => checkForNewSharedLists());
}


void dispose() {
  timer?.cancel();
  super.dispose();
}

Example 2: how to rebuild widget in flutter in time intervals

import 'package:timer_builder/timer_builder.dart';

class ClockWidget extends StatelessWidget {

  
  Widget build(BuildContext context) {
    return TimerBuilder.periodic(Duration(seconds: 1),
      builder: (context) {
        return Text("${DateTime.now()}");
      }
    );
  }
  
}

Tags:

Dart Example