Special characters not working in Firestore string - google-cloud-firestore

I'm trying to pull some data from a website to my Firebase Firestore database, but I got a small problem with special characters being converted to weird characters, for example this phrase "he’s good at – finding people." is converted to this one "he’s good at – finding people.", do I have to escape those characters or what?
Code:
String url =
'some-url';
var response = await http.get(url);
String body = response.body;
List results = jsonDecode(body)['results'];
await firestore
.collection('games')
.document(results[i]['id'].toString())
.setData({
'description': results[i]['description']});

just used utf8 to decode the result string,
utf8.decode(results[i]['description'].toString().runes.toList());
Thanks for help.

Related

Upload Blob Url to Firebase Storage | Flutter

So I already read about the topic but I simply didn't understand the solutions on stack.
I came up with this code:
Im saving a url looking like this:
final String myDataUrl = file.url;
print(myDataUrl);
blob:http://localhost:51947/2952a3b1-db6a-4882-a42a-8e1bf0a0ad73
& then Im trying to add it into Firebase Storage with the putString operator, that I guessed that suited me best while reading the Documentation. I thought that I have a Url and therefore should be able to upload it like this:
FirebaseStorage.instance
.ref()
.child("bla")
.putString(myDataUrl, format: PutStringFormat.dataUrl);
But it doesn't work, it says that:
Error: Invalid argument (uri): Scheme must be 'data': Instance of '_Uri'
So Im guessing that it somehow can't format my url to one that is accepted.
What can I do different to upload a blob successfully to firebase Storage?
-----------------Answer----------------------
Answer in the comment of the answer.
You have to convert your Blob to a Uint8List & upload it like:
Future<Uint8List> fileConverter() async {
final reader = html.FileReader();
reader.readAsArrayBuffer(file!);
await reader.onLoad.first;
return reader.result as Uint8List;
}
and then put it into your Storage:
Future uploadFile(String uid) async {
if (file == null) return;
final path = "nachweise/$uid";
Uint8List fileConverted = await fileConverter();
try {
FirebaseStorage.instance
.ref()
.child(path)
.putData(fileConverted)
.then((bla) => print("sucess"));
} on FirebaseException catch (e) {
return null;
}
}
The Firebase Storage SDKs can upload local data as either a File, an array of bytes, or a base-64 encoded string. The only URLs it accepts are so-called data URLs, which start with data:// and contain the complete data of the object. They cannot upload data directly from URLs that you more commonly see, such as http:// or https://.
You'll need to first download the data from that URL to the local device, and then upload it from there.

Base 64 convert to Image and get the error Invalid character (at character 6)

I am still struggiling with this annoying error. I have base64 string which I want to convert to Image. Here is the simpliest piece of code which is doing exactly what I want (at least, I saw it in different answers and code samples on the SO). I am getting the error:
Invalid character (at character 6)
my code is:
final String encodedStr = 'https://securelink.com/cameratypes/picture/13/true';
Uint8List bytes = base64.decode(encodedStr);
and i want to disply image:
Image.memory(bytes)
Finally, I found the solution, I don't know if it is important and will be useful to anyone who is struggling like me, but I am going to help. So, it would be easy and quick because I have already converted my image to nedeed formart (my image is base64 format), i made a dumb mistake when I was trying to convert it in String again, because it is already a String and I need Uint8List format. Side note: if your api devs said it should take a cookie or any kind of auth, it should be so.
code:
Future<String> _createFileFromString() async {
final response = await http.get(
Uri.parse(
'your link here',
),
headers: {
'cookie':
'your cookie here'
});
final Uint8List bytes = response.bodyBytes;
String dir = (await getApplicationDocumentsDirectory()).path;
String fullPath = '$dir/abc.png';
print("local file full path ${fullPath}");
File file = File(fullPath);
await file.writeAsBytes(List.from(bytes));
print(file.path);
final result = await ImageGallerySaver.saveImage(bytes);
print(result);
return file.path;
}
This code saves your image in straight to the app gallery and do not display on the screen anything
If your URI that contains data after comma as it is defined by RFC-2397. Dart's Uri class is based on RFC-3986, so you can't use it. Split the string by comma and take the last part of it:
String uri = 'data:image/gif;base64,...';
Uint8List _bytes = base64.decode(uri.split(',').last);
REFERENCE: https://stackoverflow.com/a/59015116/12382178

How to parse JSON data and print on terminal in flutter?

This is my Json data :
{
"result":"0",
"school_name":"Global Academy International",
"school_code":"GAIS",
"dashboard":"{\"privilege\":[{\"activity_id\":159,\"privilege_desc\":\"V4_MYPROFILE\",\"activity_desc\":\"V4_DASHBOARD\",\"privilege_id\":1185},{\"activity_id\":159,\"privilege_desc\":\"V4_MYTIMETABLE\",\"activity_desc\":\"V4_DASHBOARD\",\"privilege_id\":1186},{\"activity_id\":159,\"privilege_desc\":\"V4_MYATTENDANCE\",\"activity_desc\":\"V4_DASHBOARD\",\"privilege_id\":1188},{\"activity_id\":159,\"privilege_desc\":\"V4_MYLEAVESTATUS\",\"activity_desc\":\"V4_DASHBOARD\",\"privilege_id\":1189},{\"activity_id\":159,\"privilege_desc\":\"V4_MEMO\",\"activity_desc\":\"V4_DASHBOARD\",\"privilege_id\":1193},{\"activity_id\":161,\"privilege_desc\":\"V4_SUBJECTWISE\",\"activity_desc\":\"V4_STUDENT_ATTENDANCE\",\"privilege_id\":1198},{\"activity_id\":161,\"privilege_desc\":\"V4_SUBJECTWISELATE\",\"activity_desc\":\"V4_STUDENT_ATTENDANCE\",\"privilege_id\":1201},{\"activity_id\":162,\"privilege_desc\":\"V4_LEAVE_REQUISITION\",\"activity_desc\":\"V4_TEACHER_LEAVE\",\"privilege_id\":1203},{\"activity_id\":164,\"privilege_desc\":\"V4_ASSIGNMENT\",\"activity_desc\":\"V4_STUDENT_ASSIGNMENT\",\"privilege_id\":1206},{\"activity_id\":166,\"privilege_desc\":\"V4_SCHOOL_CALENDAR\",\"activity_desc\":\"V4_CALENDAR\",\"privilege_id\":1208},{\"activity_id\":168,\"privilege_desc\":\"V4_LOGOUT\",\"activity_desc\":\"V4_LOGOUT\",\"privilege_id\":1210}],\"resultPrivilege\":\"0\",\"resultProfile\":\"0\",\"profile\":{\"empid\":\"EMP183\",\"name\":\"BACHIR Raji Kashkash\",\"designation\":\"TEACHER\",\"photo_path\":\"/container/school_data/GAIS/photo/Staff/EMP183.jpg\",\"department_name\":\"HIGH SCHOOL - T\"}}",
"employee_id":"EMP183",
"school_url":"http://ict.gaiqatar.com"
}
I want to print only this data from jSON data:
[{\"activity_id\":159,\"privilege_desc\":\"V4_MYPROFILE\",\"activity_desc\":\"V4_DASHBOARD\",\"privilege_id\":1185},{\"activity_id\":159,\"privilege_desc\":\"V4_MYTIMETABLE\",\"activity_desc\":\"V4_DASHBOARD\",\"privilege_id\":1186},{\"activity_id\":168,\"privilege_desc\":\"V4_LOGOUT\",\"activity_desc\":\"V4_LOGOUT\",\"privilege_id\":1210}]
How to get this data from whole jSON data ?
Import the following package:
import 'dart:convert';
Decode the json result returned and access the specific field you want:
final body = json.decode(response.dashboard);
print(body['privilege']);
Basically after parsing your initial json you need to parse the nested field as well (privileges) since it's not a valid json object. Usually I would do this from the server and send a proper json response to the client (flutter).
If this can't be done then you just have to parse the json response, walk through it and parse and nested fields as well.
Edit:
Noticed your json wasn't formatted properly this is an example code. You can run this in dartpad
import 'dart:convert';
void main() {
final jsonString = '{\"privilege\":[{\"activity_id\":159,\"privilege_desc\":\"V4_MYPROFILE\",\"activity_desc\":\"V4_DASHBOARD\",\"privilege_id\":1185},{\"activity_id\":159,\"privilege_desc\":\"V4_MYTIMETABLE\",\"activity_desc\":\"V4_DASHBOARD\",\"privilege_id\":1186},{\"activity_id\":168,\"privilege_desc\":\"V4_LOGOUT\",\"activity_desc\":\"V4_LOGOUT\",\"privilege_id\":1210}],\"resultPrivilege\":\"0\",\"resultProfile\":\"0\"}';
final parsedJson = json.decode(jsonString);
final privilege = parsedJson['privilege'];
print(privilege);
}
The problem is your json is not properly formatted. first format it properly, you can copy and paste your json in any prettify json website and try to format it properly, if problem persists then do let me know.
The Json Data
final jsonData = """{
"result":"0",
"school_name":"Global Academy International",
"school_code":"GAIS",
"dashboard":"{\\"privilege\\":[{\\"activity_id\\":159,\\"privilege_desc\\":\\"V4_MYPROFILE\\",\\"activity_desc\\":\\"V4_DASHBOARD\\",\\"privilege_id\\":1185},{\\"activity_id\\":159,\\"privilege_desc\\":\\"V4_MYTIMETABLE\\",\\"activity_desc\\":\\"V4_DASHBOARD\\",\\"privilege_id\\":1186},{\\"activity_id\\":159,\\"privilege_desc\\":\\"V4_MYATTENDANCE\\",\\"activity_desc\\":\\"V4_DASHBOARD\\",\\"privilege_id\\":1188},{\\"activity_id\\":159,\\"privilege_desc\\":\\"V4_MYLEAVESTATUS\\",\\"activity_desc\\":\\"V4_DASHBOARD\\",\\"privilege_id\\":1189},{\\"activity_id\\":159,\\"privilege_desc\\":\\"V4_MEMO\\",\\"activity_desc\\":\\"V4_DASHBOARD\\",\\"privilege_id\\":1193},{\\"activity_id\\":161,\\"privilege_desc\\":\\"V4_SUBJECTWISE\\",\\"activity_desc\\":\\"V4_STUDENT_ATTENDANCE\\",\\"privilege_id\\":1198},{\\"activity_id\\":161,\\"privilege_desc\\":\\"V4_SUBJECTWISELATE\\",\\"activity_desc\\":\\"V4_STUDENT_ATTENDANCE\\",\\"privilege_id\\":1201},{\\"activity_id\\":162,\\"privilege_desc\\":\\"V4_LEAVE_REQUISITION\\",\\"activity_desc\\":\\"V4_TEACHER_LEAVE\\",\\"privilege_id\\":1203},{\\"activity_id\\":164,\\"privilege_desc\\":\\"V4_ASSIGNMENT\\",\\"activity_desc\\":\\"V4_STUDENT_ASSIGNMENT\\",\\"privilege_id\\":1206},{\\"activity_id\\":166,\\"privilege_desc\\":\\"V4_SCHOOL_CALENDAR\\",\\"activity_desc\\":\\"V4_CALENDAR\\",\\"privilege_id\\":1208},{\\"activity_id\\":168,\\"privilege_desc\\":\\"V4_LOGOUT\\",\\"activity_desc\\":\\"V4_LOGOUT\\",\\"privilege_id\\":1210}],\\"resultPrivilege\\":\\"0\\",\\"resultProfile\\":\\"0\\",\\"profile\\":{\\"empid\\":\\"EMP183\\",\\"name\\":\\"BACHIR Raji Kashkash\\",\\"designation\\":\\"TEACHER\\",\\"photo_path\\":\\"/container/school_data/GAIS/photo/Staff/EMP183.jpg\\",\\"department_name\\":\\"HIGH SCHOOL - T\\"}}",
"employee_id":"EMP183",
"school_url":"http://ict.gaiqatar.com"
}""";
Used double \\ to scape correctly the char "...
And then:
final parsedJson = json.decode(jsonData);
print(parsedJson['dashboard']);
If you prefer, you can parse only dashboard section to filter some itens programaticaly...
final dashboardParsed = json.decode(parsedJson['dashboard']);
print(dashboardParsed);
Final result
final jsonData = """{
"result":"0",
"school_name":"Global Academy International",
"school_code":"GAIS",
"dashboard":"{\\"privilege\\":[{\\"activity_id\\":159,\\"privilege_desc\\":\\"V4_MYPROFILE\\",\\"activity_desc\\":\\"V4_DASHBOARD\\",\\"privilege_id\\":1185},{\\"activity_id\\":159,\\"privilege_desc\\":\\"V4_MYTIMETABLE\\",\\"activity_desc\\":\\"V4_DASHBOARD\\",\\"privilege_id\\":1186},{\\"activity_id\\":159,\\"privilege_desc\\":\\"V4_MYATTENDANCE\\",\\"activity_desc\\":\\"V4_DASHBOARD\\",\\"privilege_id\\":1188},{\\"activity_id\\":159,\\"privilege_desc\\":\\"V4_MYLEAVESTATUS\\",\\"activity_desc\\":\\"V4_DASHBOARD\\",\\"privilege_id\\":1189},{\\"activity_id\\":159,\\"privilege_desc\\":\\"V4_MEMO\\",\\"activity_desc\\":\\"V4_DASHBOARD\\",\\"privilege_id\\":1193},{\\"activity_id\\":161,\\"privilege_desc\\":\\"V4_SUBJECTWISE\\",\\"activity_desc\\":\\"V4_STUDENT_ATTENDANCE\\",\\"privilege_id\\":1198},{\\"activity_id\\":161,\\"privilege_desc\\":\\"V4_SUBJECTWISELATE\\",\\"activity_desc\\":\\"V4_STUDENT_ATTENDANCE\\",\\"privilege_id\\":1201},{\\"activity_id\\":162,\\"privilege_desc\\":\\"V4_LEAVE_REQUISITION\\",\\"activity_desc\\":\\"V4_TEACHER_LEAVE\\",\\"privilege_id\\":1203},{\\"activity_id\\":164,\\"privilege_desc\\":\\"V4_ASSIGNMENT\\",\\"activity_desc\\":\\"V4_STUDENT_ASSIGNMENT\\",\\"privilege_id\\":1206},{\\"activity_id\\":166,\\"privilege_desc\\":\\"V4_SCHOOL_CALENDAR\\",\\"activity_desc\\":\\"V4_CALENDAR\\",\\"privilege_id\\":1208},{\\"activity_id\\":168,\\"privilege_desc\\":\\"V4_LOGOUT\\",\\"activity_desc\\":\\"V4_LOGOUT\\",\\"privilege_id\\":1210}],\\"resultPrivilege\\":\\"0\\",\\"resultProfile\\":\\"0\\",\\"profile\\":{\\"empid\\":\\"EMP183\\",\\"name\\":\\"BACHIR Raji Kashkash\\",\\"designation\\":\\"TEACHER\\",\\"photo_path\\":\\"/container/school_data/GAIS/photo/Staff/EMP183.jpg\\",\\"department_name\\":\\"HIGH SCHOOL - T\\"}}",
"employee_id":"EMP183",
"school_url":"http://ict.gaiqatar.com"
}""";
final parsedJson = json.decode(jsonData);
print(parsedJson['dashboard']);
final dashboardParsed = json.decode(parsedJson['dashboard']);
print(dashboardParsed);
I Scaped \" because I'm storing the string into a var, if it comes from your web api, should work too.

Serializing Special Character in Flutter [duplicate]

I am building a mobile app with Flutter.
I need to fetch a json file from server which includes Japanese text. A part of the returned json is:
{
"id": "egsPu39L5bLhx3m21t1n",
"userId": "MCetEAeZviyYn5IMYjnp",
"userName": "巽 裕亮",
"content": "フルマラソン完走に対して2018/05/06のふりかえりを行いました!"
}
Trying the same request on postman or chrome gives the expected result (Japanese characters are rendered properly in the output).
But when the data is fetched with Dart by the following code snippet:
import 'dart:convert';
import 'package:http/http.dart' as http;
//irrelevant parts have been omitted
final response = await http.get('SOME URL',headers: {'Content-Type': 'application/json'});
final List<dynamic> responseJson = json.decode(response.body)
print(responseJson);
The result of the print statement in logcat is
{
id: egsPu39L5bLhx3m21t1n,
userId: MCetEAeZviyYn5IMYjnp,
userName: å·½ è£äº®,
content: ãã«ãã©ã½ã³å®èµ°ã«å¯¾ãã¦2018/05/06ã®ãµãããããè¡ãã¾ããï¼
}
Note that only the Japanese characters (value of the content key) is turns into gibberish, the other non-Japanese values are still displayed properly.
Two notices are:
If I try to display this Japanese text in my app via Text(), the same gibberish is rendered, so it is not a fault of Android Studio's logcat.
If I use Text('put some Japanese text here directly') (ex: Text('睡眠')), Flutter displays it correctly, so it is not the Text widget that messes up the Japanese characters.
If you look in postman, you will probably see that the Content-Type http header sent by the server is missing the encoding tag. This causes the Dart http client to decode the body as Latin-1 instead of utf-8. There's a simple workaround:
http.Response response = await http.get('SOME URL',headers: {'Content-Type': 'application/json'});
List<dynamic> responseJson = json.decode(utf8.decode(response.bodyBytes));
So simple!
Instead of using response.body; You should use utf8.decode(response.bodyBytes)

Flutter fetched Japanese character from server decoded wrong

I am building a mobile app with Flutter.
I need to fetch a json file from server which includes Japanese text. A part of the returned json is:
{
"id": "egsPu39L5bLhx3m21t1n",
"userId": "MCetEAeZviyYn5IMYjnp",
"userName": "巽 裕亮",
"content": "フルマラソン完走に対して2018/05/06のふりかえりを行いました!"
}
Trying the same request on postman or chrome gives the expected result (Japanese characters are rendered properly in the output).
But when the data is fetched with Dart by the following code snippet:
import 'dart:convert';
import 'package:http/http.dart' as http;
//irrelevant parts have been omitted
final response = await http.get('SOME URL',headers: {'Content-Type': 'application/json'});
final List<dynamic> responseJson = json.decode(response.body)
print(responseJson);
The result of the print statement in logcat is
{
id: egsPu39L5bLhx3m21t1n,
userId: MCetEAeZviyYn5IMYjnp,
userName: å·½ è£äº®,
content: ãã«ãã©ã½ã³å®èµ°ã«å¯¾ãã¦2018/05/06ã®ãµãããããè¡ãã¾ããï¼
}
Note that only the Japanese characters (value of the content key) is turns into gibberish, the other non-Japanese values are still displayed properly.
Two notices are:
If I try to display this Japanese text in my app via Text(), the same gibberish is rendered, so it is not a fault of Android Studio's logcat.
If I use Text('put some Japanese text here directly') (ex: Text('睡眠')), Flutter displays it correctly, so it is not the Text widget that messes up the Japanese characters.
If you look in postman, you will probably see that the Content-Type http header sent by the server is missing the encoding tag. This causes the Dart http client to decode the body as Latin-1 instead of utf-8. There's a simple workaround:
http.Response response = await http.get('SOME URL',headers: {'Content-Type': 'application/json'});
List<dynamic> responseJson = json.decode(utf8.decode(response.bodyBytes));
So simple!
Instead of using response.body; You should use utf8.decode(response.bodyBytes)