Flutter: Concurrent modification during iteration - flutter

So I know that my mistake lies in trying to change my data when im iterating through it
this is the exact error
"Expected Non-error `Concurrent modification during iteration:
Instance of 'LinkedMap<dynamic, dynamic>'.
and this is my code:
_products.forEach((key, value) {
if (key.titel == product.titel) {
_products[key] += 1;
} else {
_products[product] = 1;
}
}
but I don't really get how I would be able to modify my data without iterating through it. Any suggestions?
----------------my Attempt -------------------
var _products = {}.obs;
var temp = {}.obs;
void addProduct(Product product) {
temp = _products;
if (_products.length > 0) {
_products.forEach((key, value) {
if (key.titel == product.titel) {
temp[key] += 1;
} else {
temp[product] = 1;
}
});
}
I get the same error; I think that is because in this line:
temp = _products;
I just get the reference on _products & I don't write _products in temp

So as mmcdon suggested you need make a copy of your existing thing that you want to iterate upon & iterate through that instead of your original piece
So in my case I need to do this:
var _products = {}.obs;
var temp = {}.obs;
void addProduct(Product product) {
temp = RxMap<dynamic, dynamic>.from(_products);
if (_products.length > 0) {
temp.forEach((key, value) {
if (key.titel == product.titel) {
_products[key] += 1;
} else {
_products[product] = 1;
}
});
}
So as you can see I iterate through temp, that basically is _products & Im writing the data that I need in _products

Related

Dart non-async function return wait until for loop is complete

Here is my code
List<DocumentReference> getStacks(
List<DocumentReference> favoritePlayers,
List<DocumentReference> stacks,
) {
List<DocumentReference> newStacks = [];
for (var i = 0; i < stacks.length; i++) {
stacks[i].get().then((DocumentSnapshot stackSnapshot) {
if (stackSnapshot.exists) {
DocumentReference thisstack = stackSnapshot['player'];
for (var x = 0; x < favoritePlayers.length; x++) {
if (favoritePlayers[x] == thisstack) {
newStacks.add(stacks[i]);
}
}
}
});
if (stacks.length - 1 == i) {
return newStacks;
}
}
}
I do not have the option in the platform I am using to make the function async or Future, so I am trying to figure out a way to not return until both for loops are done. I have tried while loops and do while, but I guess I am missing something, this method is close, but it seems it still finishes before the inner loop does and wants to return nothing.
Any thoughts would be great.
In your code, the loop goes to the next operation without waiting for "stacks[i].get()" .
If the "stacks[i].get()" function is not a Future function, you can use it as follows.
List getStacks( List favoritePlayers, List stacks) {
List newStacks = [];
for (final stack in stacks) {
DocumentSnapshot stackSnapshot = stack.get();
//code
newStacks.add(stackSnapshot);
}
return newStacks;
}
If it is a future function, it should be like this;
Future<List> getStacks( List favoritePlayers, List stacks) async{
List newStacks = [];
for (final stack in stacks) {
DocumentSnapshot stackSnapshot = await stack.get();
//code
newStacks.add(stackSnapshot);
}
return newStacks;
}

How to get list from docx file?

How to determine whether a list is bulleted or numbered? I use OpenXML
In general, what will be the list determines NumberingDefinitionsPart, I thought to find out the Numbering of a certain element, but this method did not work
I am processing the list in the recommended way, but I need to know which way it is
`public void ParagraphHandle(Elements.Paragraph paragraph, StringBuilder text)
{
var docPart = paragraph.DocumentPart;
var element = paragraph.Element;
var r = element.Descendants<Numbering>().ToArray();
var images = GetImages(docPart, element);
if (images.Count > 0)
{
foreach (var image in images)
{
if (image.Id != null)
{
string filePath = _saveResources.SaveImage(image);
_handler.ImageHandle(filePath, text);
}
}
return;
}
var paragraphProperties = element.GetFirstChild<ParagraphProperties>();
var numberingProperties = paragraphProperties?.GetFirstChild<NumberingProperties>();
if (numberingProperties != null)
{
var numberingId = numberingProperties.GetFirstChild<NumberingId>()?.Val?.Value;
if (numberingId != null && !paragraph.IsList)
{
text.AppendLine("<ul>");
paragraph.IsList = true;
paragraph.List = new List();
_htmlGenerator.GenerateList(paragraph, text);
}
else
{
_htmlGenerator.GenerateList(paragraph, text);
}
}
else
{
if (paragraph.IsList)
{
text.AppendLine("</ul>");
paragraph.IsList = false;
}
_handler.ParagraphHandle(element, text);
}
}`

Flutter error: The body might complete normally

I'm trying to run this function in Flutter and I'm getting the error: The body might complete normally, causing 'null' to be returned, but the return type, 'String', is a potentially non-nullable type.
Try adding either a return or a throw statement at the end.
But if I add return ''; in the end it does not return any value, any solution?
String getWord() {
wordCounter += 1;
var rand = Random();
int wordLength = _words.length;
int randNumber = rand.nextInt(wordLength);
bool notUnique = true;
if (wordCounter - 1 == _words.length) {
notUnique = false;
return '';
}
while (notUnique) {
if (!_usedNumbers.contains(randNumber)) {
notUnique = false;
_usedNumbers.add(randNumber);
return _words[randNumber];
} else {
randNumber = rand.nextInt(wordLength);
}
}
}
The body might complete normally, causing 'null' to be returned, but the return type, 'String', is a potentially non-nullable type. Try adding either a return or a throw statement at the end. can be resolved like the below code.
Try to return only once at the end
String getWord() {
String returnString = "";
wordCounter += 1;
var rand = Random();
int wordLength = _words.length;
int randNumber = rand.nextInt(wordLength);
bool notUnique = true;
if (wordCounter - 1 == _words.length) {
notUnique = false;
}
while (notUnique) {
if (!_usedNumbers.contains(randNumber)) {
notUnique = false;
_usedNumbers.add(randNumber);
returnString = _words[randNumber];
} else {
randNumber = rand.nextInt(wordLength);
}
}
return returnString;
}

How to pass a test if expect fails

I have this code
it('This should pass anyway', function (done) {
testObj.testIt(regStr);
});
testObj
this.testIt = function (regStr) {
selector.count().then(function (orgCount) {
for (var curr = 0; curr < count; curr++) {
checkField(curr, regStr);
}
});
};
function checkField(curr, regStr) {
selector.get(curr).all(by.tagName('li')).get(0).getInnerHtml().then(function (text) {
expect(text).to.match(regStr, curr + '#ERR');
});
}
If one of these expects get a failure, test fails. How can i handle this? I mean - can i somehow count passed and failed expect()ations and return it? or, at least, dont let test break on first error.
I've tried try-catch, but nothing good happened.
it('This should pass anyway', function (done) {
try {
testObj.testIt(regStr);
} catch (e) {
console.log('#err' + e);
}
});
And then i wanted to use done(), but havent found any examples to do the similar. Can u please help me?
Sry for my english
UPD
You can return either null or a string from checkField(), join them up, and expect the array to be empty:
this.testIt = function (regStr) {
selector.count().then(function (orgCount) {
var errors = [];
for (var curr = 0; curr < orgCount; curr++) {
var e = checkField(curr, regStr);
if (e) { errors.push(e); }
}
assert.equal(0, errors.length, errors);
});
};
A cleaner approach would be to use map() to collect the data into an array:
var data = selector.map(function (elm) {
return elm.element(by.tagName('li')).getText();
});
expect(data).toEqual(["test1", "test2", "test3"]);

Change label's text with using property

I am working on my C# ADO.NET app. I have connected my SQL Server database with C# app, and I can perform simple CRUD operations. I want to make that my app open my reminder form when someone in my database have birthday, so I made my query and all persons who have birthday on today's day are in my query, and with using property from my reminder form I change label's text with name and surname of person who have birthday. Now I just dont know how to change next label's text when more than one person have birthday in my query... I dont know how to get next element in my foreach loop...
Here is my code:
Form2 forma = new Form2();
TBirthDayEntities today_born = new TBirthDayEntities();
public Form1()
{
InitializeComponent();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 1000;
timer1.Enabled = true;
timer1.Start();
}
private Boolean provjera_rodj()
{
Boolean flag = false;
int cnt = 0;
IQueryable<TBD> query;
using (var data = new TBirthDayEntities())
{
query = (from x in data.TBD
where x.BirthDay.Day == System.DateTime.Now.Day && x.BirthDay.Month == System.DateTime.Now.Month
select x);
foreach (var x in query)
{
today_born.TBD.Add(x);
cnt += 1;
flag = true;
}
}
switch (cnt)
{
case 1:
{
foreach (var x in today_born.TBD)
{
forma.p_label2 = x.FName + " " + x.LName;
}
break;
}
case 2:
{
foreach (var x in today_born.TBD)
{
forma.p_label2 = x.FName + x.LName;
forma.p_label3 = x.FName + x.LName; //wrong
}
break;
}
}
return flag;
}
private void timer1_Tick(object sender, EventArgs e)
{
Boolean flag = provjera_rodj();
if (flag == true)
{
forma.Show();
timer1.Stop();
}
}
switch (cnt)
{
case 1:
case 2:
{
var lstLabel = new List<Label>()
{
forma.p_label2
, forma.p_label3
};
for(int i = 0; i < today_born.TBD.Count; i++)
{
var x in today_born.TBD[i];
lstLabel[x].Text = x.FName + x.LName;
}
break;
}
}
EDIT:
switch (cnt)
{
case 1:
case 2:
{
var lstLabel = new List<Action<string>>()
{
new Action<string>(s =>forma.p_label2 = s)
, new Action<string>(s =>forma.p_label3 = s)
};
for(int i = 0; i < today_born.TBD.Count; i++)
{
var x = today_born.TBD[i];
lstLabel[x](x.FName + x.LName);
}
break;
}
}