Getting learning_text_recognition library to work in Flutter - flutter

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

Related

How do you add an if statement to a TimerComponent in Flame Game Flutter?

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

How to make mock data for testing in flutter?

I am testing an event file in my flutter project.
group('ClientCreateClient', () {
test('supports comparisons', () {
expect( ClientCreateClient(1, PrivateClientModel(1, List<TestModel>.filled(2, TestModel())) ), ClientCreateClient(1, PrivateClientModel(1, List<TestModel>.filled(2, TestModel()))));
});
});
in the above code I need to provide a privateClientModel which has an id and a List as it's parameters.
How can I create a mock list to achieve this?
The above code fails the test and gives me this error
Expected: ClientCreateClient:<ClientCreateClient(1, Instance of 'PrivateClientModel')>
Actual: ClientCreateClient:<ClientCreateClient(1, Instance of 'PrivateClientModel')>
So as stated in the above comments I searched those links and it looks like I needed to extend equatable with those classes and it worked out for me.

How to handle reading from database when API-request hasn't finished yet to save to database in Flutter?

For my App i'm loading the link for the background-image for each screen from my API.
Right after the query that downloads and saves the image-link, i'm querying the link from the database.
The problem now is, that the function isn't waiting for the download and saving to finish although i'm using await, therefor i get an empty result from the database and get an error from the imageloader.
Future<String> downloadAsset(String name) async {
final Map<String, String> _json = {
'mandant': Config.mandant,
'name': name
};
final query = MakePost('get_app_assets', false, _json, saveAsset);
await query.queryAPI(); // Function won't wait for this to finish
String ret = await dbAppAssets.getText(name); // Get link from database
return ret;
}
I've already tried to use .then(), but the same thing happens.
This only happens initially on the first call of each screen, but how is this normally beeing handled?
I'm using riverpod with futureProviders if that matters.
I do not know where you are using the downloadAsset function, but when you use Future on a function you should also await the function where you are using it, for example:
Future<void> _reverseScrollToTopAnimation() async {
await controller!.reverse();
_showBackToTopButton = false; // hide the back-to-top button
}
then, wherever you call it you should also await that function:
await _reverseScrollToTopAnimation();
If not the function will act as a synchronous operation even though you are using await inside the function.

Flutter testing: static method I need to mock inside code

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?

ExpectedCondition elementToBeClickable pass but element.click fail

So I'm writing e2e test for an app and I have a method to fill up an input
static async fillInput(el, text = this.randomString()) {
await el.click();
await el.clear();
await el.sendKeys(text);
}
It works great. Well it worked great until I tried it on a field that appeared after clicking on a button. I got a Failed: element not interactable: element has zero size. Okay, maybe I4m a little hurry, so my function was updated to be that
async fillEmail() {
const newEmail = CommonMethods.createRandomEmail();
const emailElement = element(by.css('.block-info input[name="email"]'));
await browser.wait(protractor.ExpectedConditions.elementToBeClickable(emailElement));
await browser.sleep(5000)
await CommonMethods.fillInput(emailElement, newEmail);
return newEmail;
}
I reaaaaally thought it was a delay thing. But no. protractor.ExpectedConditions.elementToBeClickable pass, but fillInput failed with the same error while trying to click on the element.
I checked the css properties too, founc out line-height and font-size are set to inherit and parent has everything set up. width and max-width are set to 100%, element is clearly visible and I can click on it anywhere to get the focus.
Anyone has an idea ?