arkit_flutter_plugin
arkit_flutter_plugin copied to clipboard
"The simplest code example" does not compile
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:
- Because the
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 asCupertinoApp
orMaterialApp
. Furthermore, because it has anAppbar
, the example only works withMaterialApp
. Wrapping Scaffold inMaterialApp
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.
Can you share the correct example?
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?
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:
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)),
);
}
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.