Home > OS >  Flutter http get request button. Don't need response
Flutter http get request button. Don't need response

Time:01-09

I'm looking for a simple flutter example of an elevated button with (onPressed) http request opening hardcoded url. I don't need response. I don't need browser window to appear. Target OS is Android. Thanks in advance. UPD: i don't need any output. Just need to open urls such as https://autoremotejoaomgcd.appspot.com/sendmessage?key=SECRET_KEY&message=test to trigger tasker (android app) tasks..

SOLUTION:

import 'package:http/http.dart' as http;
The button:
Expanded(child: ElevatedButton(child: const Text('Flashlight on'), onPressed: () {const url = 'https://autoremotejoaomgcd.appspot.com/sendmessage?key=SECRET_KEY&message=flashlight on';_launchHTTP(url);},),)

The function:    
void _launchHTTP(String url) async {
var response = await http.get(Uri.parse(url));
print('Response status: ${response.statusCode}');
print('Response body: ${response.body}');
}

CodePudding user response:

What's in log message? Are you sure about proper HTTP verb? Maybe this endpoint is expecting a POST message?

CodePudding user response:

import 'package:http/http.dart' as http;
The button:
Expanded(child: ElevatedButton(child: const Text('Flashlight on'), onPressed: () {const url = 'https://autoremotejoaomgcd.appspot.com/sendmessage?key=SECRET_KEY&message=flashlight on';_launchHTTP(url);},),)

The function:    
void _launchHTTP(String url) async {
await http.get(Uri.parse(url));
}
  • Related