flutter hive putAt key error when delete from list - flutter

When I delete the employee, an error occurs in the key when I update the employee's data, and I do not understand where the problem is
this error:
Unhandled Exception: RangeError (index): Index out of range: index should be less than 4: 4
this code :
await employeerBox.putAt(
index,
employeerModel(
name: name.text.toString(),
phoneNumber: int.parse(phoneNumber.text),
location: location.text.toString(),
workDate: workDate.text.toString(),
workType: workType.text.toString(),
salary: double.parse(salary.text.toString())));
index = key
in delete by key no error but in put at give an error
I tried to update the data more than once and close and open the application, but to no avail

Related

RangeError (index): Invalid value: Not in inclusive range 0..2: 3 i cant fix

i have a problem this my list
class _FitnessAppState extends State<FitnessApp> {
String img_Header =
"https://images.unsplash.com/photo-1517836357463-d25dfeac3438?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80";
List trainingImage = [
"https://images.unsplash.com/photo-1534258936925-c58bed479fcb?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1631&q=80",
"https://images.unsplash.com/photo-1575052814086-f385e2e2ad1b?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80",
"https://media.istockphoto.com/photos/picture-of-people-running-on-treadmill-in-gym-picture-id879180126?k=20&m=879180126&s=612x612&w=0&h=WZ1Iqcqv5_rNTNslUscoMg9qAUoNiDG8kWBfVnpPapQ=",
];
your variables are:
String img_Header = "https://images.unsplash.com/photo-1517836357463-d25dfeac3438?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80";
List trainingImage = [
"https://images.unsplash.com/photo-1534258936925-c58bed479fcb?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1631&q=80",
"https://images.unsplash.com/photo-1575052814086-f385e2e2ad1b?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80",
"https://media.istockphoto.com/photos/picture-of-people-running-on-treadmill-in-gym-picture-id879180126?k=20&m=879180126&s=612x612&w=0&h=WZ1Iqcqv5_rNTNslUscoMg9qAUoNiDG8kWBfVnpPapQ=",
];
the error of RangeError is happened in trainingImage variable as it is a List datatype, that's because you are trying to access an unavailable index inside a List, as example, your list trainingImage has only 3 elements which start by index 0 and ends by index 2:
print(trainingImage[0]);
// output: "https://images.unsplash.com/photo-1534258936925-c58bed479fcb?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1631&q=80"
print(trainingImage[1]);
// output: "https://images.unsplash.com/photo-1575052814086-f385e2e2ad1b?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80"
print(trainingImage[2]);
// output: "https://media.istockphoto.com/photos/picture-of-people-running-on-treadmill-in-gym-picture-id879180126?k=20&m=879180126&s=612x612&w=0&h=WZ1Iqcqv5_rNTNslUscoMg9qAUoNiDG8kWBfVnpPapQ="
if you are trying to access the index 3 you will get the error of RangeError:
print(trainingImage[3]);
// output: RangeError (index): Invalid value: Not in inclusive range 0..2: 3
the solution is to always check if an index is available before accessing it:
// as example you want to access the index 3
int index = 3;
// 1st way
if(trainingImage.asMap().containsKey(index))
{
// run this if index 3 is available
}
// 2nd way
if(index < trainingImage.length && index >= 0)
{
// run this if index 3 is available
}
The code snippet that you have provided is fine. Maybe you are accessing wrong index of the list. Can you provide more information please?
However, I'll suggest you to rename the img_Header variable to imgHeader otherwise it will show lowerCamelCase warning.

Changed a Firestore query and index link is no longer showing in console

I recently changed a query by adding a .where clause after which the Firebase index link is not showing in the console. Can this index link be retrieved in any other way?
Added the .where('active ...
let ref = db.collection("users")
.where('active', "==", true)
.orderBy("votes", "desc").limit(25);
ref.onSnapshot((querySnapshot) => { ...
Console message:
Uncaught Error in snapshot listener: FirebaseError: no matching index found.
at new e (prebuilt-89214b55-2a7b2673.js:418)
at prebuilt-89214b55-2a7b2673.js:11050
at prebuilt-89214b55-2a7b2673.js:11051
at e.onMessage (prebuilt-89214b55-2a7b2673.js:11073)
at prebuilt-89214b55-2a7b2673.js:10990
at prebuilt-89214b55-2a7b2673.js:11021
at prebuilt-89214b55-2a7b2673.js:15973
A workaround is to create a cloud function that executes the query. Calling the function will log an error that includes the url to create the index.

SqfliteDatabaseException (DatabaseException(unrecognized token: "498a" (code 1): , while compiling: DELETE FROM Products WHERE

I am trying to delete any rows that contains a certain productGuid from my SQFLite database. so I am passing a guid to a method and try to do it by this code:
await db
.rawDelete("DELETE FROM Products WHERE productGuid = $productGuid");
I am able to read and insert data from and to my database so my database is correctly initialized. I cannot find the problem so any help would be appreciated.
here is the complete error that I get:
Exception has occurred. SqfliteDatabaseException
(DatabaseException(unrecognized token: "498a" (code 1): , while
compiling: DELETE FROM Products WHERE productGuid =
c2789c01-c972-498a-9396-096c9be49d5c) sql 'DELETE FROM Products WHERE
productGuid = c2789c01-c972-498a-9396-096c9be49d5c' args []})

SqlException: Violation of ****** constraint '******'. Cannot insert duplicate key in object '******'. The duplicate key value is ******

When trying to insert users into the AspNetUsers table using EF, I got the following error:
Microsoft.EntityFrameworkCore.DbUpdateException: 'An error occurred
while updating the entries. See the inner exception for details.'
SqlException: Violation of ****** constraint '******'. Cannot insert
duplicate key in object '******'. The duplicate key value is ******.
The statement has been terminated.
That error is verbatim (it actually showed the asterisks). Before inserting the users, I explicitly deleted them, so I don't know why I'm getting this error.
Code to delete users
foreach (var user in globalUsers)
{
if (rdb.Users.Any(u => u.Email == user.Email))
{
var existingUserInDb = rdb.Users.First(u => u.Email == user.Email);
rdb.Users.Remove(existingUserInDb);
await rdb.SaveChangesAsync();
}
}
Code to add new users
foreach (var gu in globalUsers)
{
rdb.Users.Add(gu);
await rdb.SaveChangesAsync();
}
The issue was the following:
When adding the users, I had set the User IDs explicitly
We were using soft deletes, so deleting the existing user wasn't actually deleting it
ASP.NET/EF didn't read the soft delete, and so I was inserting another user that had the same User ID as the soft deleted user
I solved the issue by creating a new User ID when inserting the users.
foreach (var gu in globalUsers)
{
gu.Id = Guid.NewGuid().ToString();
rdb.Users.Add(gu);
await rdb.SaveChangesAsync();
}

Unique compound index in MongoDB throwing DuplicateKeyException on first time insertion

I have created a compound index on a collection in mongo db using mongo-java-driver(3.6.3).
IndexOptions indexOptions = new IndexOptions().unique(true);
mongoCollection.createIndex(Indexes.ascending("f1","f2"), indexOptions);
On insertion of a record,
mongoCollection.insertOne(dBObject);
The driver is throwing a DuplicateKeyException even though there is no record in the database with same f1 and f2.
E11000 duplicate key error collection: db-test.test1 index: f1_1_f2_1 dup key: { : "testf1", : "testf2" }
If I check the record in the database through mongodb-client, the record is inserted in the database.
Yet the application code gets the exception.