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
Related
I'm still learning about Flutter with firebase.
So, I want to make a to-do-list (TDL) app, that can shows TDL item in the home page.
TDL can have 1 of these 3 status (selesai [finished], belum [not yet], telat [late]),
and i wanted to show the ones which status is belum and telat, or simply the ones which status is not equal to selesai, and has the idProject equal to "0".
Simply i need to show TDL which has idProject = "0" and status!="selesai".
So i wrote this:
static Stream<QuerySnapshot> getDataTDL() {
return CToDoList
.where("status",isNotEqualTo:"selesai")
.where("idProject",isEqualTo: "0")
.snapshots();
}
here's a quick look on my data at firebase.. So, basically it should at least return this one -actually there's few more datas it can return- because it matched my where-clause requirements, but in fact it returns nothing at all.
So what can i do now?
As your query contains both a not equal and an is equal comparison, you will have to create a composite index for the query. If you check the error logs of the flutter process, Firestore should give you a link you could use to create the index. If you can't find the link you would have to manually create it in the Firebase console for the project. More info is here.
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
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.
I am trying to update a string with firebase swift but I am getting an error that I do not know how to get rid of.
I have this code part that is getting an error:
self.dbRef.child("feed-items/\(dataPathen)/likesForPost").updateChildValues("likesForPost": "7")
The error I am getting is expected "," seperator just before the :. I am using dbRef in another code part so I know i works and the dataPathen is being printed just before the above code part, so that is working too.
Can anyone help me with this bug?
Just change
self.dbRef.child("feed-items/\(dataPathen)/likesForPost").updateChildValues("likesForPost": "7")
To
self.dbRef.child("feed-items/\(dataPathen)/likesForPost").updateChildValues(["likesForPost": "7"])
And if you are only looking for incrementing a particular value at a specific node you might wanna check my answer's :- https://stackoverflow.com/a/39465788/6297658, https://stackoverflow.com/a/39471374/6297658
PS Prefer runTransactionBlock: to update properties like likeForPosts as there might be a moment when two users try to like same post at the same moment (Highly Unlikely, but still a possibility...),using updateChildValues might end up just updating like only from one user. But runTransactionBlock: keep firing until the changes of that thread have been committed to the node
updateChildValues accepts [AnyHashable:Any] dictionary:
self.dbRef.child("feed-items/\(dataPathen)/likesForPost")
.updateChildValues(["likesForPost": "7"])
Whenever updating values at any reference in Firebase Database, you need to pass a dictionary parameter for updateChildValues method as [AnyHashable: Any] for your path reference. So just update your code of line as below:
self.dbRef.child("feed-items/(dataPathen)/likesForPost").updateChildValues("likesForPost": "7")
Also if you need to update more than 1 key-value pairs then you can pass those key-value pairs inside dictionary by seperating using comma as below:
self.dbRef.child("feed-items/(dataPathen)/likesForPost").updateChildValues(["likesForPost": "7", "otherKey": "OtherKeyValue"])
I'm working on a GUI using PyQt5 and a QTableWidget containing CheckBoxes items. I populate the table with data from json file and the user can check/uncheck items.
When I want to retrieve data from my QTableWidget items, I get unexpected error due to some characters : \x19. The character in question is ’ which is well displayed elsewhere.
I don't understand the issue as my input json data is utf-8, I use python3 and when I check my data, I see no encoding issue. This error occurs only when calling text() method on QTableWidgetItem.
As I don't see any other way to retrieve data from my items, I'm quite stuck on it.
with open(filepath) as file:
self.data = json.load(file)
for key, value in self.data.items():
print(key, self.data[key])
keyword_box = QTableWidgetItem(key)
I get no error if I use key to compare with other string for example but when I want to retrieve data (no editing was performed by the user, strings didn't change)
items = self.table.selectedItems()
for item in items:
print(item.text())
I get unknown character in my console and if I try to use the value, I get a \x19 character which fails when I use it to compare with keys in my dictionary.
Could this come from a bug in QTableWidgetItem text() method ?