olexale / arkit_flutter_plugin

ARKit Flutter Plugin
MIT License
796 stars 225 forks source link

Plugin is not initialized properly #173

Open alevittoria opened 2 years ago

alevittoria commented 2 years ago

When I start ARKitSceneView the view is totaly black with this log error:

flutter: _platformCallHandler call onError 1   arkit_plugin                        0x000000010490f26c $s12arkit_plugin16FlutterArkitViewC14onMethodCalledyySo0cG4CallC_yypSgXEtF + 732: plugin is not initialized properly
flutter: _platformCallHandler call updateAtTime {time: 131261.16993950002}
flutter: _platformCallHandler call updateAtTime {time: 131261.20329804166}
flutter: _platformCallHandler call updateAtTime {time: 131261.21997004165}
flutter: _platformCallHandler call updateAtTime {time: 131261.23665216667}
flutter: _platformCallHandler call updateAtTime {time: 131261.25331029168}
flutter: _platformCallHandler call updateAtTime {time: 131261.26999308335}
flutter: _platformCallHandler call updateAtTime {time: 131261.28666950003}
flutter: _platformCallHandler call updateAtTime {time: 131261.30334141667}
flutter: _platformCallHandler call updateAtTime {time: 131261.32002250003}
flutter: _platformCallHandler call onError 1   arkit_plugin                        0x000000010490f26c $s12arkit_plugin16FlutterArkitViewC14onMethodCalledyySo0cG4CallC_yypSgXEtF + 732: plugin is not initialized properly

This is my code

import 'package:arkit_plugin/arkit_plugin.dart';
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';

class ArKitView extends StatefulWidget {
  final String filePath;

  const ArKitView(this.filePath, {Key? key}) : super(key: key);

  @override
  State<ArKitView> createState() => _ArKitViewState();
}

class _ArKitViewState extends State<ArKitView> {
  ARKitController? arKitController;
  ARKitReferenceNode? node;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Container(
        constraints: BoxConstraints.expand(),
        child: ARKitSceneView(
          showFeaturePoints: true,
          planeDetection: ARPlaneDetection.horizontal,
          onARKitViewCreated: onArKitViewCreated,
          debug: kDebugMode,
        ),
      ),
    );
  }

  @override
  void dispose() {
    arKitController?.dispose();
    super.dispose();
  }

  void onArKitViewCreated(ARKitController controller) {
    this.arKitController = controller;
    arKitController!.addCoachingOverlay(CoachingOverlayGoal.horizontalPlane);
    arKitController!.onAddNodeForAnchor = _handleAddAnchor;
  }

  void _handleAddAnchor(ARKitAnchor anchor) {
    if (anchor is ARKitPlaneAnchor) {
      _addPlane(arKitController!, anchor);
    }
  }

  void _addPlane(ARKitController arKitController, ARKitPlaneAnchor anchor) {
    if (node != null) {
      arKitController.remove(node!.name);
    }
    node = ARKitReferenceNode(url: widget.filePath);
    arKitController.add(node!, parentNodeName: anchor.nodeName);
  }
}
zaynashahzad24 commented 2 months ago

Did you ever find a solution for this? I have the same problem.

olexale commented 2 months ago

I guess I might know what's wrong. Please check the fix by referencing the plugin in the pubspec.yaml file as follows:

  arkit_plugin:
    git:
      url: https://github.com/olexale/arkit_flutter_plugin
      ref: bugfix/init-race-condition

Looking forward to the feedback.