I am building an app which displays results it reads from a .json file. In that app I have a header-object, which stores the is the "table of contents"-object. In that object I have a list of other objects which are the results I want to display. I tried to just instantiate that object whenever I opened that page with calling Header().results.map(...);, but IMHO this is extremely dodgy and inelegant. When I tried to instantiate the object with var ToC = new Header(); before runApp(myApp); as follows:
void main() {
var ToC = new Header();
print(ToC);
runApp(
MaterialApp(
home: MyTabs(),
),
);
}
but when running ToC.results.map((result) => MeasCard(result: result)).toList() in the Dart file that should generate the List I just got the error message:
Undefined name 'ToC'
How do I instantiate such an object on app startup? Or is there another method to call the constructor just once?
Related
I am trying to import multiple GLTF files into my Three.js scene using a LoadingManager, but I am encountering an issue where I can't access the properties of the loaded models. The code seems to be working since I have them loaded correctly. I have stored the loaded models in an object called "loadedModels" but when I try to access them, it gives me an error "Cannot read properties of undefined."
This is my current code. As I said, it loads the files correctly but when I try, for instance, to change each model's coordinates I get the error described before. I tried to put the forEach function inside a setTimeout to check if the problem was that the models were not loading fast enough for me to access it, but that didn't work.
//create the toLoad const where I will type each file url
const toLoad = [
{name: 'monkey', group: new THREE.Group(), url: '3D/monkey.gltf'},
{name: 'plane', group: new THREE.Group(), url: '3D/plane.gltf'}
]
//Create empty object to store the loaded models
const loadedModels = {};
//Create a loadingManager for progress bar
const loadingManager = new THREE.LoadingManager(() => {});
//Create loader loop from multiple local urls
const gltfLoader = new GLTFLoader(loadingManager);
toLoad.forEach(item=>{
gltfLoader.load(item.url, (model)=>{
item.group.add(model.scene);
scene.add(item.group);
loadedModels[item.name] = item.group;
});
});
I'm trying to run some image processing and MLmodel prediction inside isolate for all images in the gallery device. But some packages like FaceDetector(from google_ml_kit), and PhotoGallery, don't let me run inside isolate. The question is, exist any possibility to run everything inside an isolate, like an entire package with these imports?
Without using isolate all the code is working and is kind of fast but the screen freezes when I start to load and process the images.
I also could process one by one image whitout freezing, but takes too much time.
So I m trying to run with isolate for example when a run this code:
void process(String path){
final options = FaceDetectorOptions(
performanceMode: FaceDetectorMode.accurate,
minFaceSize: .4,
);
final faceDetector = FaceDetector(options: options);
var file = File(path);
var image = InputImage.fromFile(file);
var bytes = file.readAsBytesSync();
var faces = (await faceDetector.processImage(image))
.map(
(e) => FaceRect(
e.boundingBox.left,
e.boundingBox.top,
e.boundingBox.width,
e.boundingBox.height,
),
)
.toList();
var faceImage = FacesImage(path, faces, bytes);
}
compute(process, "/storage/emulated/0/WhatsApp/Media/WhatsApp Images/IMG-20221022-WA0006.jpeg");
I get this:
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Binding has not yet been initialized.
E/flutter (32478): The "instance" getter on the ServicesBinding binding mixin is only available once that binding has been initialized.
E/flutter (32478): Typically, this is done by calling "WidgetsFlutterBinding.ensureInitialized()" or "runApp()" (the latter calls the former). Typically this call is done in the "void main()" method. The "ensureInitialized" method is idempotent; calling it multiple times is not harmful. After calling that method, the "instance" getter will return the binding
But I can't call WidgetsFlutterBinding.ensureInitialized() in isolate and my app already have.
There is any alternative to find all photos and albuns that can be called in isolate? and faceDetector?
Run an entire package in isolate is a possibility?
I'm trying to add code to my Flame Game to check if a list isn't empty and if it isn't, then send it to a function. However, I'm receiving an error on the if statement that says "Expected an identifier". How do I change my code to run an if statement here? Additionally, how would I cancel the Timer after it runs?
var instructions = [];
myGame(){
add(
TimerComponent(period: 2, repeat: true, onTick: () =>
if(instructions != null){populateInfo(instructions)}),
);
}
You can use runtimeType, Use runtimeType to get the runtime type.
A property of the Object class, which is the base class of all objects in Dart, and of type Type.
for (final element in gameRef.children) {
if (element.runtimeType == Instructions) {
//my element exist in the scene
}
}
I used the last code for explain the use of runtimeType, but with Dart you have more options like to
children.query<Intructions>();
I'm trying to add the learning_text_recognition library to my Flutter project. I was able to get the example in the API docs to work with no problems (https://pub.dev/packages/learning_text_recognition/example), but now I'm trying to add it to my own project using the information found on the Readme tab of the same website. It's slightly different than how the example worked and I'm now receiving several errors that I didn't receive in the example. Specifically, the errors are on the following line:
RecognizedText result = await textRecognition.process(image);
It says that the await function can only be used in an async function, but I don't know if I should make the function or the class async? It also says that the method 'process isn't defined for the type 'TextRecognition', but I don't know what the method should be, since that part worked perfectly fine in the example. It was also complaining that image wasn't defined, but I just created a variable called image with InputCameraView, which seemed to work.
I've tried moving the code into a new function and made an image variable. This is what the code looks like now:
getInfo(){
var image = InputCameraView(
canSwitchMode: false,
mode: InputCameraMode.gallery,
title: 'Text Recognition',
onImage: (InputImage image) {
// now we can feed the input image into text recognition process
},
);
TextRecognition textRecognition = TextRecognition();
RecognizedText result = await textRecognition.process(image);
}
I've also included the following import statements:
import 'package:learning_input_image/learning_input_image.dart';
import 'package:learning_text_recognition/learning_text_recognition.dart';
import 'package:provider/provider.dart';
I'm not sure if I'm maybe missing a step?
Your function should have the async keyword to indicate that there will be wait points. See the dart async/await documentation.
Another detail for the example InputCameraView is a widget, it should not be inside the function. It must be using the onImage method of the InputCameraView to collect the recognition and the builder to build it. In the doc onImage calls the async function _startRecognition to collect the data you must do something in this line.
void getInfo() async {
var image = InputCameraView(
canSwitchMode: false,
mode: InputCameraMode.gallery,
title: 'Text Recognition',
onImage: (InputImage image) {
// now we can feed the input image into text recognition process
},
);
var textRecognition = TextRecognition();
var result = await textRecognition.process(image);
}
I want to test a method that is responsible for a button tap (let's call it onButtonTap()), one of the first methods is a call to static method from utils file, that returns true of false, depending on set android/ios permissions (or allows user to change permissions by showing dialog that can open application settings). Let's call it checkOrRequestPermissions(). This makes everything behind that code untestable, as I don't know how to test it - I can't mock this class because:
It's not injected anywhere - it's inside utils file
It's static
So for better visualization lets go like this:
Code from file I want to test:
Future<void> onButtonTap(BuildContext context) async {
bool isGranted = await PermissionsUtil.checkOrRequestPermissions([some_args]);
// CODE_A - some code I want to test
}
Code inside PermissionsUtil:
class PermissionsUtil{
static Future<bool> checkOrRequestPermissions([some_args]){
// code for permissions
}
}
So my questions are:
Is there any way I could mock checkOrRequestPermissions() to simply return given value?
How could I make this code testable?