i am using django as a backend to flutter - flutter

when i get data from API Arabix words doesn't show and it show like this
is this from flutter or Django Restframework?
flutter request
'''
final url = Uri.parse('http://127.0.0.1:8000');
final data = await http.get(url);
final flats = json.decode(data.body) as List<dynamic>;
if(flats==null) {
return;
}
print(flats)
'''

The solution was to use dio instead of http.

Related

Flutter Web - Error code: XML Http Request Error

I am trying to fetch a simple JSON file from my HTTPS server in flutter web however, it doesn't fetch the JSON and simply prints :-
Error code: XMLHTTPRequest error
How do I fix this issue? Here's my code -
Future<List<GamesJSON>> fetch(String link) async {
final res = await get(Uri.parse(link));
if (res.statusCode == 200) {
final data = json.decode(res.body);
final rest = data;
list = rest.map<GamesJSON>((json) => GamesJSON.fromJson(json)).toList()
as List<GamesJSON>;
}
return list;
}
You need to change this line
list = rest.map<GamesJSON>((json) => GamesJSON.fromJson(json)).toList()
as List<GamesJSON>;
to this
List<GamesJSON> list = rest.map<GamesJSON>((json) => GamesJSON.fromJson(json)).toList();

why it is not possible to scrape data from this website "https://www.migros.com.tr/" in flutter

I tried a lot but was not able to scrape any data from this website. is there any way to scrape data from this website? https://www.migros.com.tr/
I am using HTML and HTTP libraries in flutter.
class Scraper {
static void getData() async {
String URL = "https://www.migros.com.tr/";
final response = await http.get(Uri.parse(URL));
final body = response.body;
final html = parse(body);
final names = html.querySelectorAll('.product-name');
final images = html.querySelectorAll('.image >a >img');
print(images[1].atrributes['src'].trim());
print(names[1].text.trim());
}
}

convert JSON from google app script to flutter

i'm trying to fetch data from a range of two columns on google sheet
first column with numbers,second column text
to my flutter app.
i managed to make the the http request
by this line of code:
var url = Uri.parse(
'https:`enter code here`//script.googleusercontent.com/macros/echo?user_content_key=Bzv-qcg70rUkHCr4pjI_k_qlB9c5I_GjKS-U726WCslGZ0tulYSbfdD1DabRoVrrbDSn9rIS78vQxr33OOOjaAw4d4DcA8sGm5_BxDlH2jW0nuo2oDemN9CCS2h10ox_1xSncGQajx_ryfhECjZEnBqJjZp2jp3I5Qlxq6dcbGTdqr4FTByb3YAAvkZxH-A03NfLQ3Ce8hucRs86AXu4Vcg63MBXANpH4BVmytCxmg24Mg9dF6sKjQ&lib=M7v_2CxFrrqdcmzthOkMrc1jilJU4QU4H');
void getData() async {
Response response = await get(url);
if(response.statusCode == 200) {}
var feedback = convert.jsonDecode(response.body);
print('Response body: ${response.body}');
}
when i'm printing the response body,i'm getting the whole data
now,what i'm trying to do is to enter a number to a TextField in the flutter app
and to get a result from the parallel second column next to the first column where is typed the number i entered.
=============================
update
i've created a model from my json data
this the model created from the website
https://javiercbk.github.io/json_to_dart/
class Note {
late String id;
Note({required this.id});
Note.fromJson(Map<String, dynamic> json) {
id = json['id'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['id'] = id;
return data;
}
}
now this the new code for getting the data from google sheet
Future<void> getData() async {
var response = await http.get(Uri.parse(
"https://script.google.com/macros/s/AKfycbzU89ZEE0Y9sgEQZMZHSbX00M6hPdHOX6WN8IjQWa5lzgAzmAc4jZShpUfKbnJ5zm8J/exec"));
var body = response.body;
Note note = Note.fromJson(jsonDecode(body));
print(note.id.toString());
}
and i'm getting an error when trying to run the flutter app on vscode
a new tab of browser client opened no data come from the google sheet api
and this a screenshot from the vscode
While working with JSON data
The good practice is to create a model for that and then just fetch the data through the API
creating a model and a class is easy enough and doesn't take effort and makes your work easy ;)
For creating a model for your project
VISIT https://javiercbk.github.io/json_to_dart
Just copy your JSON data and paste in the textField and you will get your Model Class ready with just one click
for accessing the data
Test _test = Test.fromJson(response.body);
that's it.
refer image shown below

Autofill login credentials in flutter inappWebview

I am using flutter_inappwebview plugin for developing an android browser. Now I need to save and autofill login credentials for different websites like facebook, twitter etc.
I come to know about securely storing login data using flutter_secure_storage plugin but I don't have any knowledge about how to implement this.
I want this feature exactly as it has in Google Chrome. Please help if you can...
Thanks in advance!
flutter_secure_login currently does not support Flutter platform web.
Since this is the case I would recommend using shared_preferences found here
Its very similar to secure storage and can be utilized in a very similar way
in the following code i am using it to store my user data
// write to shared preferences
final prefs = await SharedPreferences.getInstance();
if (!_context.isLoggedIn) {
if (prefs.containsKey('userData')) prefs.remove('userData');
return;
}
final userData = Map<String, String>();
userData['refreshToken'] = _refreshToken;
userData['tenantId'] = _tenantId;
userData['userId'] = _userId;
await prefs.setString('userData', json.encode(userData));
That can then be accessed to refresh tokens.
Future init() async {
final prefs = await SharedPreferences.getInstance();
final prefData = prefs.getString('userData');
if (prefData == null) return;
final Map<String, Object> userData = json.decode(prefData);
_authWriter.write({
'refreshToken': userData['refreshToken'],
'userId': userData['userId'],
'tenantId': userData['tenantId']
});
await refreshAccessToken();
}

uploading image to backed from flutter error 401

Flutter image upload to laravel backed is returning 401 error. My backed have database fields name, middelName , and profilePic The api works fine on postman but on flutter picking image from gallery and camera its not working i have implemented internet permission on all manifests i used Vpn but error 401
here is flutter code
Future uploadImage() async{
final uri =Uri.parse("https://www.compaytarining.com/api/exrecise2/");
var request =http.MultipartRequest('POST',uri);
request.fields["name"]=nameController.text;
request.fields["middelName"] = mnameController.text;
var pic= await http.MultipartFile.fromPath("profilePic", _image.path);
request.files.add(pic);
var response = await request.send();
print(response.statusCode);
if(response.statusCode==200)
{
print('ImageUploaded');
}
else
{
print("Error");
}
}
Gallery Image
File _image;
final picker=ImagePicker();
Future getImage() async
{
final pickedImage=await picker.getImage(source: ImageSource.gallery);
setState(() {
_image=File(pickedImage.path);
});
}
Many thanks
Hello check it may be error is from your response which is when you return an api response may be u set it to return 401.