arkit_flutter_plugin icon indicating copy to clipboard operation
arkit_flutter_plugin copied to clipboard

"The simplest code example" does not compile

Open crcdng opened this issue 3 years ago • 5 comments

The initial example on https://pub.dev/packages/arkit_plugin does not compile with null-safe Flutter, two issues:

  • while the library is null safe, the example is not. It therefore does not compile in null-safe Flutter. Two changes:

    • ARKitController? arkitController;
    • Because the position parameter takes a `Vector3?, something like:

Vector3? pos = Vector3(0, 0, -0.5); and providing pos to position fixes the problem.

  • the example then throws a "No MediaQuery ancestor could be found starting from the context that was passed to MediaQuery.of()". This is because Scaffold needs an ancestor such as CupertinoApp or MaterialApp. Furthermore, because it has an Appbar, the example only works with MaterialApp. Wrapping Scaffold in MaterialApp fixes this issue.

*Note that flutter automaticaly imports 'package:vector_math/vector_math.dart' instead of 'package:vector_math/vector_math64.dart' - this needs to be edited manually to fix another error that comes up.

After these modifiucations it works. I would recommend to test example code.

crcdng avatar Jul 11 '21 17:07 crcdng

Can you share the correct example?

take2make avatar Aug 09 '21 14:08 take2make

Hi. The example from the README works fine for me on Flutter 2.2.3 and the latest plugin. @i3games , does the example app works for you?

olexale avatar Aug 10 '21 09:08 olexale

it does not work with null-safe Flutter

Hi. The example from the README works fine for me on Flutter 2.2.3 and the latest plugin. @i3games , does the example app works for you?

while the library is null safe, the example is not. It therefore does not compile in null-safe Flutter. Two changes:

crcdng avatar Aug 10 '21 09:08 crcdng

Can you share the correct example?

import 'package:arkit_plugin/arkit_node.dart';
import 'package:arkit_plugin/geometries/arkit_sphere.dart';
import 'package:arkit_plugin/widget/arkit_scene_view.dart';
import 'package:flutter/material.dart';
import 'package:vector_math/vector_math_64.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  ARKitController? arkitController;
  Vector3? position = Vector3(0, 0, -0.5);

  void onARKitViewCreated(ARKitController arkitController) {
    this.arkitController = arkitController;
    final node = ARKitNode(
        geometry: ARKitSphere(radius: 0.1),
        position: position); // , position: Vector3(0, 0, -0.5)
    this.arkitController?.add(node);
  }

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

  @override
  Widget build(BuildContext context) => MaterialApp(
        home: Scaffold(
            appBar: AppBar(title: const Text('ARKit in Flutter')),
            body: ARKitSceneView(onARKitViewCreated: onARKitViewCreated)),
      );
}

crcdng avatar Aug 10 '21 09:08 crcdng

I've updated the README with a working (at least for me) sample and ported the example app to null safety. Will push a new version 1.0.3 to pub.dev soon.

olexale avatar Aug 10 '21 18:08 olexale