Home > Software engineering >  The return type 'Null' isn't a 'Future<_>', as required by the closur
The return type 'Null' isn't a 'Future<_>', as required by the closur

Time:09-02

The return type 'Null' isn't a 'Future<_>', as required by the closure's context. What the problem here ?

    void insertToDatabase() {
    database.transaction((txn){
      txn.rawInsert
        ('INSERT INTO tasks (title,date,time,status)VALUES("First task","0011","2430","New")')
          .then((value) {
        print('$value Inserted');
      }).catchError((error){
        print('error is ${error.toString()}');
      });

       return null;
    });
  }

CodePudding user response:

Fortunately i got solved it : go to pubspec.yaml 2)find this- environment: sdk: ">=2.12.0 <3.0.0" 3)change it environment: sdk: ">=2.11.0 <3.0.0"

CodePudding user response:

Please forget that then exists and embrace the async/await style, it will make many things easier:

Future<void> insertToDatabase() async {
    await database.transaction((txn) async {
      try {
        final value = await txn.rawInsert
        ('INSERT INTO tasks (title,date,time,status) VALUES ("First task","0011","2430","New")');

        print('$value Inserted');
      } catch(error) {
        print('error is ${error.toString()}');
      }
    });
  }

You "solution" just downgrades Flutter so it cannot warn you about your problem anymore. It still is the same problem though, it did not go away.

  • Related