Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I do what the example shows? #515

Closed
jinsw1081 opened this issue Jan 6, 2022 · 2 comments
Closed

How can I do what the example shows? #515

jinsw1081 opened this issue Jan 6, 2022 · 2 comments

Comments

@jinsw1081
Copy link

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.

@WouterVandenputte
Copy link

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

@timbotimbo
Copy link
Collaborator

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. #732

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants