RangeError (index): Index out of range: no indices are valid: 0 - flutter

I got this when I was using Hive Database in Flutter.
Find out answer below

I was getting the same error. when I was trying to add data into hive database using putAt(), but from documantion i found out that putAt() can only be used for the existing index. so switch putAt() instruction with add(). That solved the error.
I know the question is old, but I hope it could help someone.

problem for me was, I am accessing the empty list with index.
ex:
list=[ ] //this is empty list
list[0] // error, because I am accessing empty list.

So whenever you get this kind of Error!
Check if whatever you are saving in Hive Database is not null.
I got this error while I was storing null object to database.

you should give and initializer for example if you want to give and array you should set a default value for example :
List<dynamic> _users = [];
this return error
but if you do this your problem will be solve :
List<dynamic> _users = [
{
"id":"",
"name:"",
"Phone:""
}
]
depends of your data that fields

Related

Query In MongoDb while Insertion

I use a function insertOne to insert an object. I have place correct colon and also correct commas then why this error come. Thanks in Advance.
This is my code
db.items.insertOne({Name:"Oppo",Price:"45000",Ritems:"345",Sold:"45","Rating:"3.5",once"twos"})
Error is this
uncaught exception: SyntaxError: missing : after property id :
#(shell):1:78
>
I think that your "" are misplaced. I think this way would be better ??
db.items.insertOne({Name:"Oppo",Price:"45000",Ritems:"345",Sold:"45",Rating:"3.5",once:"twos"})
Also the error message is telling you about the Id column, you should check if you need to set it yourself or if your database has an auto-increment ID.

Flutter save array to Firebase Database

I am making a small example with Flutter, the data is saved on Firebase Database Realtime as an array, on the client side I will download the array, display it on a list view then customize it, end the process I want to save directly that array on Firebase. But when I use the following code
myRef.set(myArray);
And error:
Unhandled Exception: Invalid argument: Instance of...
I tried converting my array to json array by the following code
String jsonArray = jsonEncode(myArray);
myRef.set(jsonArray);
then the data was set successfully, but the result was a string that appeared on Firebase, not a list I wanted.
So I can ask questions here, expect a correct answer from everyone, very respectfully
Okay i got an answer
I just create an Map<String,dynamic> to repersent for each item in myArray and put it to List like that
List<Map<String,dynamic>> result = new List<Map<String,dynamic>>();
myArray.forEach((item) {
result.add(item.toJson());
});
And final, i just set 'result' for Firebase
stepRef.set(result);
Thank you for reading :D

Flutter Moor join and where

My Dependencies for moor:
moor_flutter: ^2.1.1
moor_ffi: ^0.4.0
I have the tables:
netPoint = Information about the netPoint
netPointNetPoint = linking of netpoints
I want all netPoints that match the "PARENTS_ID" s. below.
My problem is that I cannot access netPointNetPoint in the Where condition.
select(netPoint)
..where((t) => t.TYPE.equals(type))
..join(
[
innerJoin(netPointNetPoint, netPointNetPoint.CHILDREN_ID.equalsExp(netPoint.ID)),
innerJoin(netPointNetPoint, netPointNetPoint.PARENTS_ID.equals(parentId))
]
)
).get();
Unfortunately the help page https://moor.simonbinder.eu/docs/advanced-features/joins/ did not help me.
Well, the questions is quite old, but I've faced the same problem and just in case some one will have the same problem. I guess you had to change the expressions with something like this:
select(netPoint)
..where((t) => t.TYPE.equals(type))
..where((t) => netPointNetPoint.PARENTS_ID.equals(parentId))
..join(
[
innerJoin(netPointNetPoint, netPointNetPoint.CHILDREN_ID.equalsExp(netPoint.ID))
]
)).get();
The join's result will have all the columns you need, hence you just need to check that the column in the row has needed value. E.g. netPointNetPoint.PARENTS_ID.equals(parentId).
I guess, you are accessing the netPointNetPoint table outside the extended Database class. That will fail as all tables are accessed inside the database.
and I guess, you initialized that database in such a way it is accessible by other files.
if you initialized the database like this :
final AppDatabase db = AppDatabase();
then, to reference your netPointNetPoint table, just do it from the Database object, like this :
db.netPointNetPoint
I think, this will help you

'Invalid document reference. Document references must have an even number of segments' in flutter

I am using firestore with flutter. I am constantly getting this error and not able to solve it despite reading all available reference.Can Someone please help me solve the problem.
My Code -
String uid = '+919101006470';
final snapShot = await db.collection("users").document(uid).get();
The error is
[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(error, Invalid document reference. Document references must have an even number of segments, but users has 1, null)
The cause of the error is quite simple, you have a path with an uneven number of segments, which means it points to a collection and not to a document.
The easiest way to troubleshoot is to print the path of the document reference before calling get() on it:
final ref = db.collection("users").document(uid).get();
print (ref.path);
final snapShot = await ref;
As Doug commented, on the code you gave it seems most likely that uid doesn't have a value, but printing the path will show you which segment of your path is empty/missing.

Flutter Firestore getting incomplete array

I'm trying to fetch an array of strings I have saved on my database but I'm getting the array back with missing values, only the first value is shown inside of it. Here's the structure of my database:
Path: /users/uid/services <-- this is the array
Path: /services/uid <--service document
The code I'm using to retrieve the users is:
_getWorkers() async {
var query = await Firestore.instance.collection('users').where('services', arrayContains: widget.category['uid']).getDocuments();
query.documents.forEach((doc) {
workers.add(doc.data);
List<String> values = List.from(doc.data['services']);
print('services:' + values.toString());
});
var test = await Firestore.instance.collection('users').document('PtBD2EMSTodvlx6WHUx8QOHHLNA2').get();
print('actual services:' + test['services'].toString());
}
Both query and test get data back, but the services array only contains the first value.
Its difficult to answer without actually seeing the entire DB structure. But still on the outset i can see only one error with your code.
When you are trying to execute the where() call, try adding the snapshots to retrieve all relevant data.
Since await is also used, it is much better to call the listen() on it and then read the values to be added to the worker
Try using this below code in place of your first line.
await Firestore.instance.collection('users').where('services', arrayContains: widget.category['uid']).snapshots().listen((query)=>query.documents.forEach((doc)=>print(doc.data['services'])));
For some reason Firebase wasn't returning the updated array. Tested it again today and it worked with the same code. Sorry for bothering, thanks for the help anyways.