I’m performing tests in flutter and this error is occurring (LateInitializationError: Field ‘iCategory’ has not been initialized.
test\interfaces\icategory_test.dart
iCategory
test\interfaces\icategory_test.dart:1
main.)
//iterfaces/icategory.dart
import '../models/categorias/category_model.dart';
abstract class ICategory {
Future<List<CategoryModel>> fetchCategory(int page);
}
//iterfaces/icategory_test.dart
// ignore_for_file: avoid_print
import 'package:flutter_test/flutter_test.dart';
import 'package:app/interfaces/icategory.dart';
late ICategory iCategory;
void main() {
test('Teste iCategory', () {
print(iCategory);
});
}
//Error - LateInitializationError: Field 'iCategory' has not been initialized.
//test\interfaces\icategory_test.dart iCategory
//test\interfaces\icategory_test.dart:1
//main.<fn>
>Solution :
You haven’t assign the value. First you need to create a class implementing ICategory.
class CategoryRepoImpl implements ICategory {
@override
Future<List<CategoryModel>> fetchCategory(int page) async {
// your logic.....
return [];// list of data
}
}
void main() {
late ICategory iCategory;
setUp(() {
iCategory = CategoryRepoImpl();
});
test('Teste iCategory', () {
print(iCategory);
});
}
You can check more about mocking