I wrote a torrent download demo , look like this:
var peers....
for(var i =0;i < length;i++){
var peer = peers[i];
peer.start((List<int> data){
_downloadFile.write(begin,end,data);
});
}
I mean that there are mutiple peer try to access the _file.
`_downloadFile` code looks like this:
....
var _access = _file.open(mode:FileMode.writeOnlyAppend);
...
Future write(int begin,int end, List<int> block) async{
....
await _access.setPosition(begin);
await _access.writeFrom(block);
....
}
And it will throw exception : FileSystemException: An async operation is currently pending
I think the reason is that different fork invoke setPosition, so I try to make each fork to wait the write operation over:
`_downloadFile` code look like this:
....
var _access = _file.open(mode:FileMode.writeOnlyAppend);
var _lock;
...
Future write(int begin,int end, List<int> block) async{
if(_lock != null) await _lock;
var c = Completer();
_lock = c.future;
....
await _access.setPosition(begin);
await _access.writeFrom(block);
c.complete();
....
}
If I understand correctly, when peer recieves data , it will call write method , then it should wait the _lock completed, but I still get FileSystemException.
Now I have to use Stream , with its pause and resume method to avoid this exception.
Can someone tell me what's the problems with the last codes? why the _lock does not work? Does Dart have something like 'object lock'?
Thanks very much!
Related
From a basic standpoint what I am trying to do is get a list of keys (key names) from session storage.
The way I am trying to do this is by calling the JsRuntime.InvokeAsync method to:
Get the number of keys in session storage, and
loop thought the number of items in session storage and get the key name.
public async Task<List<string>> GetKeysAsync()
{
var dataToReturn = new List<string>();
var storageLength = await JsRuntime.InvokeAsync<string>("sessionStorage.length");
if (int.TryParse(storageLength, out var slength))
{
for (var i = 1; i <= slength; i++)
{
dataToReturn.Add(await JsRuntime.InvokeAsync<string>($"sessionStorage.key({i})"));
}
}
return dataToReturn;
}
When calling the JsRuntime.InvokeAsync($"sessionStorage.length")) or JsRuntime.InvokeAsync($"sessionStorage.key(0)")) I am getting an error "The value 'sessionStorage.length' is not a function." or The value 'sessionStorage.key(0)' is not a function.
I am able to get a single items using the key name from session storage without issue like in the following example.
public async Task<string> GetStringAsync(string key)
{
return await JsRuntime.InvokeAsync<string>("sessionStorage.getItem", key);
}
When I use the .length or .key(0) in the Chrome console they work as expected, but not when using the JsRuntime.
I was able to get this to work without using the sessionStorage.length property. I am not 100% happy with the solution, but it does work as needed.
Please see below code. The main thing on the .key was to use the count as a separate variable in the InvokeAsync method.
I think the reason for this is the JsRuntime.InvokeAsync method adds the () automatically to the end of the request, so sessionStorage.length is becoming sessionStorage.length() thus will not work. sessionStorage.key(0) was becoming sessionStorage.key(0)(). etc. Just that is just a guess.
public async Task<List<string>> GetKeysAsync()
{
var dataToReturn = new List<string>();
var dataPoint = "1";
while (!string.IsNullOrEmpty(dataPoint) )
{
dataPoint = await JsRuntime.InvokeAsync<string>($"sessionStorage.key", $"{dataToReturn.Count}");
if (!string.IsNullOrEmpty(dataPoint))
dataToReturn.Add(dataPoint);
}
return dataToReturn;
}
I am trying to create a method to convert a List to Future<List>.
This is the method I created.
static Future<List<Product?>> fromProductRefList(
List<DocumentReference> ref) async {
List<Product> shopProductList = [];
ref.forEach((productRef) async {
final productDoc = productRef.get();
final product = await Product.fromDocument(await productDoc);
shopProductList.add(product!);
});
print('shopProductList: $shopProductList');
return shopProductList;
}
and called it in cubit,
void mapProductToState() async {
emit(state.copyWith(status: MyProductStatus.loadding));
final shop = _shopBloc.state.shop;
List<Product?> productList = [];
if (shop.shopProductRef.isNotEmpty) {
final productList = Product.fromProductRefList(shop.shopProductRef);
}
emit(state.copyWith(
shop: shop,
productList: productList,
status: MyProductStatus.loaded,
));
}
VScode shows no error but when I run the code, fromProductRefList return empty list. Seems like fromProductRefList did not wait for the Document actually get() from the database and just return.
When I add a second delay in fromProductRefList before returning the shopProductList, everything works as expected.
I have read another question on stackoverflow suggest using asyncMap() but I am not sure how to apply it in my case.
Edit:
When I add a delay, the method return without any issue. If not, it will return a empty list
static Future<List<Product?>> fromProductRefList(
List<DocumentReference> ref) async {
List<Product> shopProductList = [];
ref.forEach((productRef) async {
final productDoc = productRef.get();
final product = await Product.fromDocument(await productDoc);
shopProductList.add(product!);
});
await Future.delayed(const Duration(milliseconds: 500));
print('shopProductList: $shopProductList');
return shopProductList;
}
Thank you.
You are missing the await keyword, to actually wait for the call. It only compiles, because you also declare a new variable of name productList, shadowing the already existing one.
So this line:
final productList = Product.fromProductRefList(shop.shopProductRef);
should read:
productList = await Product.fromProductRefList(shop.shopProductRef);
In addition, this does not do what you think it does:
ref.forEach((productRef) async {
It does not wait for each call. Please use a normal for flow control structure and await the async call, not the forEach method. The forEach method will no wait for the Futures returned from the methods.
I'm getting green swirly lines underneath the await keyword.
When I hover the mouse over the green swirly lines, it says 'await' applied to 'Query', which is not a 'Future'.
I'm not sure to which statement the await keyword needs to be applied.
Without applying the await/async and Future to the method the calling method doesn't wait for this method to get completed and continues with executing the rest of the code, therefore returning a null value.
Please find below the code which is in question:
Future<List<ContData>> readCC(String tID) async {
List<ContData> conList = [];
try {
final conCollection = await FirebaseFirestore.instance
.collection('CC')
.where('conID', isEqualTo: tID);
await conCollection.snapshots().listen((snapshot) {
var tc = snapshot.docs[0].data()['con'];
ContData con;
for (var t in tc) {
con = ContData.fromMap(data: t);
print(con);
final conType = con.conType;
final conText = con.conText;
final conCodeCaption = con.conCodeCaption;
final conCodeText = con.conCodeText;
final conCodeOutput = con.conCodeOutput;
final conImageFilename = con.conImageFilename;
final conImageCaption = con.conImageCaption;
final conImageCredit = con.conImageCredit;
final conImageCreditLink = con.conImageCreditLink;
final conAnchorText = con.conAnchorText;
final conAnchorLink = con.conAnchorLink;
final ContData = ContData(
conType: conType,
conText: conText,
conCodeCaption: conCodeCaption,
conCodeText: conCodeText,
conCodeOutput: conCodeOutput,
conImageFilename: conImageFilename,
conImageCaption: conImageCaption,
conImageCredit: conImageCredit,
conImageCreditLink: conImageCreditLink,
conAnchorText: conAnchorText,
conAnchorLink: conAnchorLink);
}
});
} catch (e) {
print(e.toString());
}
return conList;
}
Thank you so much for your precious time and help in advance!
You can only add await to a method that returns a Future, for example:
final conCollection = await FirebaseFirestore.instance
.collection('CC')
.where('conID', isEqualTo: tID).get();
The method get() returns a Future<QuerySnapshot> therefore instead of using then() which takes a callback that will be called when the future is completed.
Read the following guide:
https://dart.dev/codelabs/async-await
I am using Tessaract's Xamarin Forms Nuget(https://github.com/halkar/Tesseract.Xamarin), and am trying to scan a picture taken by the Android Device. This is the code that I am using:
private async System.Threading.Tasks.Task<string> OCRAsync(byte[] bytes)
{
TesseractApi api;
api = new TesseractApi(this, AssetsDeployment.OncePerInitialization);
await api.Init("bul");
await api.SetImage(bytes);
var detectedText = api.Results(PageIteratorLevel.Block);
result = string.Empty;
if (detectedText != null)
{
foreach (var annotation in detectedText)
{
result = FindWordInDictionary(annotation.Text);
}
}
return result;
}
The method is called from a synchronized method like this:
var task = OCRAsync(data);
result = task.Result;
Whenever the compiler gets to "await api.Init("bul");" the app freezes indefinitely. Do you know what may cause this problem? Thank you.
The problem was that I needed to give a file location in the .init function:
await api.Init(pathToDataFile, "bul");
I am having problems with futures in a Flutter app.
void saveCats() async {
var cats = await convertToCats(_rawData);
await DatabaseProvider.db.addCats(cats);
}
Future<List<Cat>> convertToCats(CatList catData) async {
var cats = <Cat>[];
await catData.forEach(key, value) async {
var pos = await calculatePos();
print('This should come first');
cats.add(Cat(name: key, pos: pos);
}
}
Future<int> calculatePos() async {
return await DatabaseProvider.db.calculatePos();
}
database.dart:
Future<void> addCats(List<Cat> cats) async {
print('This should come second');
// Do adding stuff here
}
Future<int> calculatePos() async {
// Some code to calculate the position
return pos;
}
In the above code, the saveCats function is called when a button is tapped. This function converts some raw data to a list of Cat models, and adds them to the database. As part of this conversion process, it calculates the pos of the cat in a list. My problem is that I would expect that, of my two print statements, the one in the forEach loop should come before the one in the addCats database function. But instead they appear in reverse order. Where am I going wrong?
You can't async/await in List.forEach() or Map.forEach() as both of them return void.
Either use
await Future.forEach([1, 2, 3], (num) async {
await asyncMethod(num);
});
or something similar;
https://api.flutter.dev/flutter/dart-async/Future/forEach.html
forEach often doesn't do what you expect, because the provided function runs as a closure.
It's more natural when you want to iterate over a list doing something to each element to use for (or one of the more functional type methods like map).
It's not clear what type CatList is, so this is approximate, but you'll want something more like:
Future<List<Cat>> convertToCats(CatList catData) async {
var cats = <Cat>[];
for (var i = 0; i < catData.length; i++) {
var key = catData[i].key;
var pos = await calculatePos();
print('This should come first');
cats.add(Cat(name: key, pos: pos));
}
return cats;
}
or
Future<List<Cat>> convertToCats(CatList catData) async {
return catData.map(...some mapping function...).toList();
}