'Dart unit tests fail to run on Github Actions

I have a dart project with unit tests. When I run the unit tests locally they run successfully.

I added CI to the project with Github Actions. The same unit tests that run successfully locally, fail on Github Actions with the following error:

00:00 +0: loading test/foo_test.dart
00:01 +0: loading test/foo_test.dart
00:01 +0 -1: loading test/foo_test.dart [E]
  Failed to load "test/foo_test.dart":
  Couldn't resolve the package 'foo' in 'package:foo/foo.dart'.
  test/foo_test.dart:5:8: Error: Not found: 'package:foo/foo.dart'
  import 'package:foo/foo.dart';
         ^
  test/foo_test.dart:33:26: Error: Method not found: 'Bar'.
      final bar = Bar(
          ^^^^^^^^^^^^

00:01 +0 -1: Some tests failed.
Error: Process completed with exit code 1.

foo_test.dart imports foo.dart as a package (not relative path).

import 'package:foo/foo.dart'


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source