flutter-unity-view-widget icon indicating copy to clipboard operation
flutter-unity-view-widget copied to clipboard

How can I do what the example shows?

Open jinsw1081 opened this issue 3 years ago • 1 comments

Describe the bug image**To

Steps to reproduce the behavior: I followed what is shown in the example.

  1. Fix Ndk
  2. Put the Unity folder inside your flutter project
  3. Install the Flutterunity project package into Unity
  4. Export through package's export android
  5. 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.

jinsw1081 avatar Jan 06 '22 08:01 jinsw1081

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();

WouterVandenputte avatar Jan 08 '22 14:01 WouterVandenputte

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

timbotimbo avatar Jan 08 '23 18:01 timbotimbo