Are functions executed by Flutter's Timer thread safe?

720 Views Asked by At

Consider following code:

class AppState extends ChangeNotifier {
  List<int> collection = [];
  
  AppState() {
    Timer.periodic(
      const Duration(seconds: 1),
      (Timer t) async => await mutateCollection(),
    );
  }
  
  Future mutateCollection() async {
    collection.add(DateTime.now().millisecond);
    notifyListeners();
  }
  
  void mutateCollectionFromUI() {
    collection.add(DateTime.now().millisecond);
    notifyListeners();
  }

  void createNewCollectionFromUI() {
    collection = [];
    notifyListeners();
  }
}

Is it thread safe?

0

There are 0 best solutions below