i want to build a crud application using sqlite and Flutter Cubit, the problem is that when i want to red data from my database it return an error. i test my query in console it work prefect but when i use with Listview it dos not working. i though maybe problem relate to my Cubit implementation but it was not,this is my code:
database red query:
Future<List<Account>> redAllAccounts() async {
final result = await db.query('account');
return result.map((e) => Account.fromjsonAccount(e)).toList();
}
my Cubit file:
import 'package:bloc/bloc.dart';
import 'package:project/Model/account.dart';
import 'package:project/control/database.dart';
class AccountCubit extends Cubit<List<Account>> {
AccountCubit(intistate) : super(intistate);
Future getAccount() async {
DatabaseHelper database = DatabaseHelper();
List<Account> account;
account = await database.redAllAccounts();
emit(account);
}
}
my list account file:
class AccountList extends StatelessWidget {
@override
Widget build(BuildContext context) {
BlocProvider.of<AccountCubit>(context).getAccount();
// var account = BlocProvider.of<AccountCubit>(context).account;
return Container(
padding: EdgeInsets.all(20),
child: BlocBuilder<AccountCubit, List<Account>>(
builder: (context, state) {
return ListView.builder(
padding: EdgeInsets.all(20),
itemCount: state.length,
itemBuilder: (context, index) {
var acc = state[index];
return Column(
children: [
Text(acc.name),
// Text(acc.adress),
TextButton(
onPressed: () {
// BlocProvider.of<AccountBlock>(context)
// .add(AccountEvent.delete(acc.accountNumber));
},
child: Text('delete'))
],
);
});
***error:
NoSuchMethodError (NoSuchMethodError: The method 'query' was called on null.
Receiver: null
Tried calling: query("account"))***