Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

LateInitializationError: Field 'iCategory' has not been initialized

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading