flutter-unity-view-widget
flutter-unity-view-widget copied to clipboard
How can I do what the example shows?
Describe the bug
**To
Steps to reproduce the behavior: I followed what is shown in the example.
- Fix Ndk
- Put the Unity folder inside your flutter project
- Install the Flutterunity project package into Unity
- Export through package's export android
- Write the code shown in the example
Screenshots Unity (please complete the following information):
- OS: window
- Version 19.4.31f
Smartphone (please complete the following information):
- Device: S10+
- OS: andorid11
Additional context
I'm working on running Unity in Flutter by copying the example as it is.
I'm new to flutter so I don't know which one is the problem.
Your problem has nothing to do with this package. As of Dart 2.12, there is sound-null-saftey. This is a rather new programming paradigm that more and more languages are shifting towards. It basically removes the need for doing checks like
if(obj != null)
In your case, you define a variable UnityWidgetController _unityWidgetController; but the type UnityWidgetController is non-nullable.
There are now two options: you either make it nullable UnityWidgetController? _unityWidgetController or you initialize it immediately which in this specific case is inpossible since it is provded by the package when unity is created. This is how I have it
static UnityWidgetController? _controller;
Widget build(BuildContext context) {
return UnityWidget(
borderRadius: BorderRadius.zero,
onUnityCreated: _onUnityCreated,
onUnityMessage: _onUnityMessage,
onUnitySceneLoaded: _onSceneLoaded,
);
}
void _onUnityCreated(UnityWidgetController controller) {
print('Unity view created');
_controller = controller;
}
Since your field is nullable, all methods performed on it must be done by calling _controller?.method() or (if you know for sure that is not null)
if(_controller!=null) _controller!.method();
Null-safety in the examples is likely to get merged in soon. Meanwhile anyone that can't figure it out can look at the updated example code in this pull request. https://github.com/juicycleff/flutter-unity-view-widget/pull/732