0

I can't able to store snapshot.data to database via floor in Flutter. I wrote entity, dao and database file, builded database and database.g.dart succesed to complete, but when I tried to insertUser function it turns below error; What am I missing? Is there anything to do for record future snapshot.data which there isn't in [the guide]?1

Error:

════════ Exception caught by gesture ═══════════════════════════════════════════════════════════════
The following NoSuchMethodError was thrown while handling a gesture:
The method 'insertUser' was called on null.
Receiver: null
Tried calling: insertUser(Instance of 'UserF')

My entity:

import 'package:floor/floor.dart';

@entity
class UserF {
 @PrimaryKey(autoGenerate: true)
 final int id;

 final String user;
 final int overview;


 UserF({this.id,
     this.user,
     this.overview,


 @override
 int get hashCode => id.hashCode ^ user.hashCode ^ overview.hashCode  ;

 @override
 String toString() {
   return 'UserF{id: $id, user: $user, overview: $overview}';
 }
}

DAO:

import 'package:floor/floor.dart';
import 'entity.dart';

@dao
abstract class UserDao {
 @Query('SELECT * FROM UserF')
 Future<List<UserF>> findAllUsers();

 @Query('SELECT * FROM UserF WHERE id = :id')
 Stream<UserF> findUserById(int id);

 @insert
 Future<void> insertUser(UserF userF);

 @delete
 Future<int> deleteUser(UserF userF);

}

Database:

import 'dart:async';
import 'package:floor/floor.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart' as sqflite;
import 'user_dao.dart';
import 'entity.dart';
part 'database.g.dart'; // the generated code will be there

@Database(version: 1, entities: [UserF])
abstract class AppDatabase extends FloorDatabase {
 UserDao get userDao;
}

Related Parts on my main.dart

Future<void> main() async{
 WidgetsFlutterBinding.ensureInitialized();

 final AppDatabase = await $FloorAppDatabase
     .databaseBuilder('database.db')
     .build();

 runApp(MyApp());
}

....

floatingActionButton: FloatingActionButton(
                         onPressed: (){
                           final userf = UserF(user: snapshot.data.user, overview: snapshot.data.overview);
                           favoriteDao.insertUser(userf);
                         },
                         child: Icon(Icons.add),
....

Lunedor
  • 1,410
  • 1
  • 14
  • 33
  • Does this answer your question? [What is a NoSuchMethod error and how do I fix it?](https://stackoverflow.com/questions/64049102/what-is-a-nosuchmethod-error-and-how-do-i-fix-it) – nvoigt Oct 15 '20 at 06:12
  • `favoriteDao` is `null`. Since you did not post any code showing us that it shouldn't be, the best guess is you forgot to initialize it. – nvoigt Oct 15 '20 at 06:13

1 Answers1

1

If the code :

part 'database.g.dart';

is creating error that means you have to generate that file.

Add these dependencies if you haven't already:

Dependencies:

floor: ^0.14.0
sqflite: ^1.3.0

Dev Dependencies:

floor_generator: ^0.14.0
build_runner: ^1.8.1

In terminal run the following command:

flutter packages pub run build_runner build

And wait for some time. Flutter will generate the command. Flutter will automatically generate the file.

REMEMBER: THE NAME OF THE DATABASE FILE AND NAME OF GENERATED FILE MUST BE SAME EXEPT FOR ADDING .g

For Example

if the database file name is mydatabase.dart the generated file name must be mydatabase.g.dart

Mhamza007
  • 151
  • 1
  • 4