0

I was following this stack-overflow to implement a timer operations at a one-second interval and is getting the captioned error. What do I have to do?

  floatingActionButton: FloatingActionButton(
    child: Icon(Icons.add),
    onPressed: () async {
      Timer.periodic(Duration(seconds:1), (Timer t) => print(t));
    }
  );

Error:

lib/main.dart:182:48: Error: 'Timer' isn't a type.                      
      Timer.periodic(Duration(seconds:1), (Timer t) => print(t));   
                                           ^^^^^                    
lib/main.dart:182:11: Error: The getter 'Timer' isn't defined for the class '_UploadPageState'.
 - '_UploadPageState' is from 'package:decg_main/main.dart' ('lib/main.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'Timer'.
          Timer.periodic(Duration(seconds:1), (Timer t) => print(t)); 
neic
  • 145
  • 1
  • 10

1 Answers1

3

I found the solution here: https://developermemos.com/posts/create-repeating-timer-flutter

  1. Have to import 'dart:async';

  2. Have to add Timer timer = prior to the Timer.periodic() function.

neic
  • 145
  • 1
  • 10