I'm working on a Flutter web project. The web/index.html
file is injecting a JavaScript script which could be simplified as:
<script>
window['dataLayer'] = [];
</script>
So in my flutter code, I can do:
@JS()
import 'package:js/js.dart';
@JS('dataLayer.push')
external void push(data);
push(myData);
to push myData
in the window.dataLayer
array.
However, in the tests, when I'm running
flutter test --platform chrome
I'm getting the error
TypeError: Cannot read properties of undefined (reading 'push')
when I call
push(myData);
because window['dataLayer']
has never been created.
How can I inject some javascript scripts the same way I can do in the index.html
?