Flutter - vector_math defined in Two Places

Updated vector_math dependency to latest (2.1.0) in a flutter project and project now fails to build with error:

The argument type 'Vector2 (where Vector2 is defined in /mnt/data/work/flutter/.pub-cache/hosted/)' can't be assigned to the parameter type 'Vector2 (where Vector2 is defined in /mnt/data/work/flutter/.pub-cache/hosted/)'. (Documentation)
Vector2 is defined in /mnt/data/work/flutter/.pub-cache/hosted/ (vector2.dart:10).
Vector2 is defined in /mnt/data/work/flutter/.pub-cache/hosted/ (vector2.dart:10).

How does one resolve this? Is this a flutter issue or is issue in vector_math package?

2

2 Answers

Check the packages that were imported, the error is due to the the types being imported from different libraries.

vector_math and vector_math_64 should be for 32bit and 64bit variants, they are implemented as two different libraries in the package. So you'd need to maintain consistency in how you the libraries are used.

A possible solution to not face this issue in the future is to export the types from a base package and import that types as needed from the base_package, this will protect users of the base_package from needing to know the what types are required or having this issue again.

For example: lib/base_package.dart

export 'package:vector_math/vector_math_64.dart'
or
export 'package:vector_math/vector_math.dart'

Changing to

import 'package:vector_math/vector_math_64.dart';

fixed it for me.

Another solution would be to scope it...

import 'package:vector_math/vector_math.dart' as whatever;

Note, make sure you do it everywhere that you import!

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like