Salim Dziri
Salim Dziri

Reputation: 21

How to use a variable inside a URL in flutter

Why this is working?

final response = await http.get(Uri.parse("http..../?page=2"),

And this is not working?

int p = 2;
String page = p.toString();
final response = await http.get(Uri.parse("http..../?page="+p),

Upvotes: 0

Views: 390

Answers (2)

Maciej Szakacz
Maciej Szakacz

Reputation: 594

For this purpose Uri.https has a Map<String, dynamic>? queryParameters, property.

You can use it this way:

  final p = 2;
  final parameters = <String, String>{
     'page': p.toString(),
    };
  
  
  final uri = Uri.http(
    'http....com',
    '/',
    parameters,
  );
  final response = await httpClient.get(uri);

Upvotes: 1

Inan Mahmud
Inan Mahmud

Reputation: 244

Do it like this

final response = await http.get(Uri.parse("http..../?page=$p"),

Upvotes: 1

Related Questions